zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    AjaxHelper 无刷新留言
    girdview 中的radiobutton 的逐行触发checkedselected事件
    .NET 新语法
    获取checkbox的值
    git 代码提交规范
    chrome的timeline中stalled问题解析
    小程序添加节流阀
    深度遍历与广度遍历
    JS 运行机制
    地址栏输入url后做了那些事情什么
  • 原文地址:https://www.cnblogs.com/zhcnblog/p/2626495.html
Copyright © 2011-2022 走看看