DataGridView DataGridViewCheckBoxColumn编辑时实时触发事件
正常响应CellValueChanged()事件时,当改变checkbox状态时,只有当焦点离开该单元格时才能触发CellValueChanged()事件,
如果要改变checkbox值时实时触发CellValueChanged()事件,需要借用CurrentCellDirtyStateChanged()事件来提交未提交控件的更改。
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
这样CellValueChanged()事件就可以随着checkbox的值的改变实时触发;
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.RowIndex != -1 && !dataGridView1.Rows[e.RowIndex].IsNewRow)
{
if (e.ColumnIndex == 7)
{
if ((bool)this.dataGridView1[e.ColumnIndex, e.RowIndex].Value == true)
{
this.dataGridView1[1, e.RowIndex].Value = 100;
}
else
{
this.dataGridView1[1, e.RowIndex].Value = 10;
}
}
}
this.toolStripStatusLabel1.Text = e.ColumnIndex.ToString() + "," + e.RowIndex.ToString();
}