DataGridViewCheckBoxColumn的问题

环境:VS2013

系统:win7 32位

问题:在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()))
                    {
                         ……
                    }
                }
            }

解决方法(来源于http://www.cnblogs.com/gossip/archive/2008/12/02/1346047.html):

在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();

 

posted @ 2015-07-28 18:55  了了之  阅读(2560)  评论(0编辑  收藏  举报