zoukankan      html  css  js  c++  java
  • C# winfrom 中datagridview中checkbox的使用方法

    方法一:
    private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
                int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
                for (int i = 0; i < count; i++)
                {
                    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
                    Boolean flag = Convert.ToBoolean(checkCell.Value);
                   if (flag == true)     //查找被选择的数据行
                    {            
                        checkCell.Value = false;
                    }
                    else
                       continue;
                }
          }
    }

    获取选择的数据

              int count = Convert.ToInt32(dgv_zy.Rows.Count.ToString());
              for (int i = 0; i < count; i++)
                {
                    //如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
                 dgv_zy.EndEdit();
                    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
                   Boolean flag = Convert.ToBoolean(checkCell.Value);
                    if (flag == true)     //查找被选择的数据行
                   {
                       //从 DATAGRIDVIEW 中获取数据项
                    string z_zcode = dgv_zy.Rows[i].Cells[0].Value.ToString().Trim();

                    }
                }

    方法二:

    如果需要在winform 的数据控件datagridview 中嵌入checkbox列 (  DataGridViewCheckBoxCell ),
    在程序的执行中有可能需要像纯粹的checkbox控件的selectedindexchanged事件一样的事件来捕捉其状态的改变

    我觉得比较好的方式是用datagridview 控件的cellcontentclick事件   例如:

    如果嵌入的 DataGridViewCheckBoxCell 列在第一列,判断状态并添加处理事件可以为:

      private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {

                if (e.ColumnIndex == 0 && e .RowIndex != -1)
                {

                         //获取控件的值

                        MessageBox.Show(this.dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString());

                    //或者可以做其他事件处理程序

                }

    }

    需要注意的是执行此事件是需要屏蔽其他datagridview单元格的cellcontentclick事件 ,即让除了 DataGridViewCheckBoxCell 列

    之外的所有列的ReadOnly=True;

      在获取datagridview中checkbox列的值得时候 一定要用 EditedFormattedValue属性,此属性获取的是编辑以后数值 而value 和

    FormattedValue返回的往往是编辑以前的数值,而其重复单击的时候往往会出现错误(无法确定是编辑前还是编辑后的数值: 主要

    原因是焦点问题,需要先移动焦点使datagridview获取更改后的数据在区获取他 就没有问题了,所以以后用去获取数据前先要移出

    datagridview中的焦点!!!),所以一定要用EditedFormattedValue来获取属性值

    http://www.cnblogs.com/yang5830963/archive/2009/06/11/1501175.html

  • 相关阅读:
    二分查找
    Android手机APN设置(中国移动 联通3G 电信天翼),解决不能上网的问题
    cocos2dx 3.1从零学习(三)——Touch事件(回调,反向传值)
    Groovy新手教程
    总结一下用caffe跑图片数据的研究流程
    JAVA数组的定义及用法
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    LeetCode——Reverse Words in a String
    oracle 库文件解决的方法 bad ELF interpreter: No such file or directory
    布局文件提示错误“No orientation specified, and the default is horizontal. This is a common so...”
  • 原文地址:https://www.cnblogs.com/emanlee/p/1528074.html
Copyright © 2011-2022 走看看