码迷,mamicode.com
首页 > Windows程序 > 详细

关于datagridview中checkbox列在选中行的情况下无法操作值

时间:2015-02-13 13:13:54      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

这几天做项目的时候碰到了个小问题,在datagridview中实现对checkbox列的全选和反选功能。代码如下

             //全选
             if (dataGridView1.Rows.Count > 0)
                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {
                    (dgvr.Cells["checkbox列名称"] as DataGridViewCheckBoxCell).Value = true;
                }
             //反选
              if(dataGridView1.Rows.Count>0)
                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).Value = (bool)((dgvr.Cells["DeleteFile"] as DataGridViewCheckBoxCell).Value) ? false : true;
                }
    发现个问题就是在datagridview选中的那一行上checkbox的值并没有改变。经baidu和google了一下,发现在代码改变状态前,使其readonly变为true就能解决。原理不得而知,先记下来吧!改进后的代码如下:
             //全选
             if (dataGridView1.Rows.Count > 0)
                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).ReadOnly = true;
                    (dgvr.Cells["checkbox列名称"] as DataGridViewCheckBoxCell).Value = true;
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).ReadOnly = false;
                }
             //反选
              if(dataGridView1.Rows.Count>0)
                foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                {         
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).ReadOnly = true;
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).Value = (bool)((dgvr.Cells["DeleteFile"] as DataGridViewCheckBoxCell).Value) ? false : true;
                    (dgvr.Cells[" checkbox列名称 "] as DataGridViewCheckBoxCell).ReadOnly = false;
                }

关于datagridview中checkbox列在选中行的情况下无法操作值

标签:

原文地址:http://www.cnblogs.com/goodmangis/p/4290046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!