美文网首页
C#DataGridViewCheckBoxColumn触发Ch

C#DataGridViewCheckBoxColumn触发Ch

作者: 堆石成山 | 来源:发表于2023-03-09 21:32 被阅读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

    相关文章

      网友评论

          本文标题:C#DataGridViewCheckBoxColumn触发Ch

          本文链接:https://www.haomeiwen.com/subject/nclzldtx.html