C#DataGridViewCheckBoxColumn触发Ch

2023-03-09  本文已影响0人  堆石成山

在C#DataGridView中添加了一个DataGridViewCheckBoxColumn,发现在点击CheckBox的时候,一是没有找到CheckedChanged事件,二是并没有触发CellValueChanged事件。
如何获取DataGridView中CheckBox的值改变事件呢,解决方案如下:

//在CurrentCellDirtyStateChanged提交checkbox的值改变
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
            //提交改变,触发dataGridView1_CellValueChanged事件,以便及时获取check的值改变事件
            if (dataGridView1.IsCurrentCellDirty)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
//值改变事件
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
         //获取选中行号
         int  selectedIndex= dataGridView1.SelectedCells[0].RowIndex;
          //获取Checkbox改变的值,假如checkbox是在第5列
         bool checkValue=bool.Parse(dataGridView1.Rows[selectedIndex].Cells[4].Value.ToString())
}

参考:https://zhuanlan.zhihu.com/p/336385251

上一篇下一篇

猜你喜欢

热点阅读