zoukankan      html  css  js  c++  java
  • 关于datagridview中checkbox列在选中行的情况下无法操作值

    这几天做项目的时候碰到了个小问题,在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;
                    }
  • 相关阅读:
    揭秘:如何为 Kubernetes 实现原地升级
    阿里云叔同:以容器为代表的云原生技术,已经成为释放云价值的最短路径
    如何画好一张架构图?(内含知识图谱)
    K8s 资源全汇总 | K8s 大咖带你 31 堂课从零入门 K8s
    15-16年总结——拨开云雾终见青天
    从一个程序员的角度看——微信小应用(第一弹 初学)
    h5直播开发之旅总结
    初探和实现websocket心跳重连(npm: websocket-heartbeat-js)
    组件化h5活动模板的实现
    总结JavaScript事件机制
  • 原文地址:https://www.cnblogs.com/goodmangis/p/4290046.html
Copyright © 2011-2022 走看看