我正在使用CellClick事件,并希望更新网格上的另一个复选框。 示例: - 包含“添加”和“删除”列的两列。 用户单击“添加”,系统将检查是否也未选中“删除”复选框。 如果选中删除 ------将Delete设置为false
换句话说,不能同时检查同一行的复选框“添加”和“删除”。
我正在使用......
private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
如果我是新手如何以对象的形式访问单元格,我应该能够完成其余的工作。
我一直在寻找使用cmbBox = e.Control as ComboBox
的例子,但这不起作用:(
链接到示例将有助于感谢。
从建议的编辑添加到答案-Andomar
这有效......
private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
//Set a var that determined whether or not the checkbox is selected
bool selected = (bool)this.customersDataGridView[e.ColumnIndex, e.RowIndex].Selected;
//Do the flip-flop here
const int add = 4;
const int delete = 5;
switch (e.ColumnIndex)
{
//If the checkbox in the Add column changed,
// flip the value of the corresponding Delete column
case add:
this.customersDataGridView[delete, e.RowIndex].Value = !selected;
break;
//If the checkbox in the Delete column changed,
// flop the value of the corresponding Add column
case delete:
this.customersDataGridView[add, e.RowIndex].Value = !selected;
break;
}
}
}
不需要dataGridView1_CellMouseUp
答案 0 :(得分:2)
尝试使用CellValueChanged事件而不是CellClick事件。下面的处理程序将根据其对应复选框的相反值(即同一行中的复选框)设置复选框的值。
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
//Set a var that determined whether or not the checkbox is selected
bool selected = (bool)this.dataGridView1[e.ColumnIndex, e.RowIndex].Value;
//Do the flip-flop here
switch (e.ColumnIndex)
{
//If the checkbox in the Add column changed, flip the value of the corresponding Delete column
case 0:
this.dataGridView1[1, e.RowIndex].Value = !selected;
break;
//If the checkbox in the Delete column changed, flop the value of the corresponding Add column
case 1:
this.dataGridView1[0, e.RowIndex].Value = !selected;
break;
}
}
}
//You may need to do something goofy like this to update the DataGrid
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
this.dataGridView1.EndEdit();
}
}