我有一个在其上添加了DataGridViewCheckBoxColumn的DataGridView。我还添加了两个按钮,一个用于选择所有checkBox,另一个用于取消选择所有checkBox。
单元格单击和选择/取消选择所有按钮单独起作用,但不起作用,例如,我用鼠标单击选中一个单元格(在UI上选中复选框),然后如果我按下取消选择-全部按钮,则checkboxs的值改变,但UI上的checkboxs保持选中状态。
这是我的代码单元格单击
private void dictionaryDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == dictionaryDataGrid.Columns["ColSel"].Index) && (e.RowIndex >= 0))
dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value = !(bool)(dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value == null ? false : dictionaryDataGrid.Rows[e.RowIndex].Cells["ColSel"].Value);
}按钮全选:
private void btn_selAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = true;
}
}按钮取消全选:
private void btn_unselAll_Click(object sender, EventArgs e)
{
for (int i = 1; i <= dictionaryDataGrid.Rows.Count; i++)
{
dictionaryDataGrid.Rows[i - 1].Cells["ColSel"].Value = false;
}
}发布于 2014-10-13 17:37:20
试试这个:
private void btn_unselAll_Click(object sender, EventArgs e)
{
dictionaryDataGrid.ClearSelection();
foreach (DataGridViewRow row in dictionaryDataGrid.Rows)
{
((DataGridViewCheckBoxCell)row.Cells["ColSel"]).Value = false;
}
}https://stackoverflow.com/questions/26336627
复制相似问题