当前位置:首页 > Windows程序 > 正文

DataGridViewCheckBoxColumn的问题

2021-03-28 Windows程序

问题:在DataGridView中使用列样式DataGridViewCheckBoxColumn的问题:

(1)界面上通过鼠标点击,使DataGridViewCheckBoxColumn列的多个CheckBox状态发生改变后,,当鼠标仍选中DataGridViewCheckBoxColumn列的某个单元格时,获取其状态时,发现选中的单元格值获取结果错误;

获取方法:dr.Cells["CheckItem"].Value.ToString()

foreach (DataGridViewRow dr in DgvSoftUp.Rows) { if (dr.Cells["CheckItem"].Value != null) { if (Convert.ToBoolean(dr.Cells["CheckItem"].Value.ToString())) { …… } } }

解决方法(来源于):

在DataGridView的CurrentCellDirtyStateChanged事件中增加:DgvSoftUp.CommitEdit(DataGridViewDataErrorContexts.Commit);

private void DgvSoftUp_CurrentCellDirtyStateChanged(object sender, EventArgs e) { DgvSoftUp.CommitEdit(DataGridViewDataErrorContexts.Commit); }

(2)通过代码设置CheckBox状态时,当单元格选中状态时,状态设置无效。

设置代码如下:

foreach (DataGridViewRow dr in DgvSoftUp.Rows) { dr.Cells["CheckItem"].Value = true; }

解决办法,在设置之后,增加EndEdit(): 

DgvSoftUp.EndEdit();

DataGridViewCheckBoxColumn的问题

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69159.html