none
如何在datagridview中添加checkbox changechecked event? RRS feed

  • 问题

  • 我在winform中用了一个datagridview控件,其中包括checkbox控件.现在想要在checkbox选中或没有选中的时候做一些处理.试过许多datagridview事件了,都没有达到效果.请问能不能在datagirdview里面给checkbox添加一个事件?不能的话应该怎么做?想要的效果是比如选不选中checkbox都会立即调用一个方法.
    2010年5月20日 4:03

答案

  •  CellContentClick 或 CellValueChanged 都捕捉到 CheckBox 的事件,此时单元是未更改的值,如果要得到值需要,
    先在 CurrentCellDirtyStateChanged 中提交更改,然后 CellContentClick 或 CellValueChanged 中访问

    大概如下,

     private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }

            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex] == someDataGridViewCheckBoxColumn && e.RowIndex > -1)
                {              
                    MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
            }

           


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    2010年5月20日 5:25
  • 通常,复选框单元格值要用于存储(象任何其他数据一样)或执行批量操作。如果需要在用户单击复选框单元格时立即响应,您可以处理 DataGridView.CellContentClick 事件,但此事件在更新单元格值之前发生。

     

    如果您在单击时需要新值,一个选项是根据当前值计算所需的值是什么。

     

    另一种方法是立即提交更改,并处理 DataGridView.CellValueChanged 事件以对此作出响应。若要在单击单元格时提交更改,则必须处理 DataGridView.CurrentCellDirtyStateChanged 事件。在处理程序中,如果当前单元格为复选框单元格,则调用 DataGridView.CommitEdit 方法并传入 Commit 值。

     

    ---- form DataGridViewCheckBoxColumn MSDN 文档

     

    关于第二种方法, 由于DataGridView.CurrentCellDirtyStateChanged 事件 是在单元格的状态相对于其内容的更改而更改时发生。 当单元格的内容已更改,但更改尚未保存时,该单元格将标记为已修改。 此事件通常会在以下情况下发生:当单元格已编辑,但是更改尚未提交到数据缓存中时,或者当编辑操作被取消时。由此,你可以通过在CurrentCellDirtyStateChanged事件中,调用 datagridview.commitEdit() 方法,以引发CellValueChanged事件。 DataGridView.CurrentCellDirtyStateChanged 事件 MSND 文档中也提供了一个例子。


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    2010年5月21日 3:44
    版主

全部回复

  •  CellContentClick 或 CellValueChanged 都捕捉到 CheckBox 的事件,此时单元是未更改的值,如果要得到值需要,
    先在 CurrentCellDirtyStateChanged 中提交更改,然后 CellContentClick 或 CellValueChanged 中访问

    大概如下,

     private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }

            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex] == someDataGridViewCheckBoxColumn && e.RowIndex > -1)
                {              
                    MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
            }

           


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    2010年5月20日 5:25
  • 通常,复选框单元格值要用于存储(象任何其他数据一样)或执行批量操作。如果需要在用户单击复选框单元格时立即响应,您可以处理 DataGridView.CellContentClick 事件,但此事件在更新单元格值之前发生。

     

    如果您在单击时需要新值,一个选项是根据当前值计算所需的值是什么。

     

    另一种方法是立即提交更改,并处理 DataGridView.CellValueChanged 事件以对此作出响应。若要在单击单元格时提交更改,则必须处理 DataGridView.CurrentCellDirtyStateChanged 事件。在处理程序中,如果当前单元格为复选框单元格,则调用 DataGridView.CommitEdit 方法并传入 Commit 值。

     

    ---- form DataGridViewCheckBoxColumn MSDN 文档

     

    关于第二种方法, 由于DataGridView.CurrentCellDirtyStateChanged 事件 是在单元格的状态相对于其内容的更改而更改时发生。 当单元格的内容已更改,但更改尚未保存时,该单元格将标记为已修改。 此事件通常会在以下情况下发生:当单元格已编辑,但是更改尚未提交到数据缓存中时,或者当编辑操作被取消时。由此,你可以通过在CurrentCellDirtyStateChanged事件中,调用 datagridview.commitEdit() 方法,以引发CellValueChanged事件。 DataGridView.CurrentCellDirtyStateChanged 事件 MSND 文档中也提供了一个例子。


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    2010年5月21日 3:44
    版主