• datagridview中加入checkbox列,全选问题


    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
             

    if (e.RowIndex < 0)
                   return;
               string[] SubCode = null;
               string Code = this.dataGridView1.Rows[e.RowIndex].Cells["FunctionCode"].Value.ToString();   //获取当前点击行的FunctionCode的值
               SubCode = Code.Split('_');  //根据FunctionCode中的‘下划线’把FunctionCode分为两部分
               if (Code == SubCode[0])     //如果code(当前选中行的FunctionCode的值)等于被拆分出来的第一个值,则执行下下语句
               {
                   if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "1")   //如果所点击行的第一列checkbox的值为1 ★★
                   {
                       for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++) //循环判断datagridview中的第一行
                       {
                           string CodeA = this.dataGridView1.Rows[i].Cells["FunctionCode"].Value.ToString(); //获取当前循环所在的行的FunctionCode
                           string[] SubCodeA = CodeA.Split('_');   //拆分出两部分
                           if(this.dataGridView1.Rows[i].Cells[0].Value == null)   //因为checkbox不接受null值,会报错,防止获取的null值报错,让其等于0
                           {
                               this.dataGridView1.Rows[i].Cells[0].Value = 0;
                           }
                           if (SubCodeA[0] == Code && this.dataGridView1.Rows[i].Cells[0].Value.ToString() == "1") //如果拆分出的第一部分的值和所点击的行的FunctionCode的值相同,并且当前循环行的checkbox值为1的话,
                           {
                               this.dataGridView1.Rows[i].Cells[0].Value = 0;      //反选为0 -----与上面加‘★★’的地方相反,因为在点击Code之前,checkbox为选中状态,所以点击一次之后,应当为不选中状态
                           }
                       }
                   }
                   else if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "0")
                   {
                       for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
                       {
                           string CodeA = this.dataGridView1.Rows[i].Cells["FunctionCode"].Value.ToString();
                           string[] SubCodeA = CodeA.Split('_');
                           if (this.dataGridView1.Rows[i].Cells[0].Value == null)
                           {
                               this.dataGridView1.Rows[i].Cells[0].Value = 0;
                           }
                           if (SubCodeA[0] == Code && this.dataGridView1.Rows[i].Cells[0].Value.ToString() == "0")
                           {
                               this.dataGridView1.Rows[i].Cells[0].Value = 1;
                           }
                       }
                   }
               }

    }

    但这样,双击(父节点时),(子节点)只执行一次。  在

    private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)

    事件中,加入相同代码,让其它单击双击都执行相同结果,即可解决。

    image image

  • 相关阅读:
    6-ESP8266 SDK开发基础入门篇--操作系统入门使用
    5-ESP8266 SDK开发基础入门篇--了解一下操作系统
    【java规则引擎】基本语法和相关属性介绍
    【eclipse】 怎么解决java.lang.NoClassDefFoundError错误
    【java规则引擎】java规则引擎搭建开发环境
    【4】JDK和CGLIB生成动态代理类的区别
    【java规则引擎】一个基于drools规则引擎实现的数学计算例子
    【3】SpringMVC的Controller
    设计模式之禅之代理模式
    【java规则引擎】规则引擎RuleBase中利用观察者模式
  • 原文地址:https://www.cnblogs.com/zhcnblog/p/2626495.html
Copyright © 2020-2023  润新知