zoukankan      html  css  js  c++  java
  • DataGridView 中添加CheckBox和常用处理方式 .

    DataGridView 中添加CheckBox和常用处理方式

    文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934

    DataGridView中添加CheckBox控件主要采用两种方法

    1.  通过在DataGridView的Columns中添加System.Windows.Forms.DataGridViewCheckBoxColumn类型的列。并可以设置该列相关的属性信息。

    2. 在程序代码中直接添加相应的代码

    System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
    Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();


    Column1.HeaderText = "Column1";
    Column1.Name = "Column1";
    Column1.ReadOnly = false;

    ...


    this.dgvRet.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {Column1});

    也可以采用this.dgvRet.Columns.Add(Column1);进行更灵活的列添加。

    在DataGridView绑定数据源后,可以通过设置DataGridViewCheckBoxColumn的DataPropertyName来确定影响CheckBox显示的数据源的属性和详细列名(具体如何影响可以由TrueValue、FalseValue和IndeterminateValue来设置)。也可以让数据源与添加的CheckBox无关联,在DataGridView中显示为不同的列。

    DataGridView 中CheckBox的常用处理方法。

    1.  如何设置CheckBox的值。     

        this.dgvRet.Rows[i].Cells[j].Value = false;

        直接设置在DataGridView第(i+1)行第(j+1)列中CheckBox的Value的值。若Value的值为true,CheckBox 被勾上,若Value的值为false,CheckBox 不勾上。

    2. CheckBox是否勾上的事件处理方法。

        需要注意的是:在DataGridView中添加DataGridViewCheckBoxColumn,CheckBox是占有整个Cell方框的。在c#中默认情况下,CheckBox是否被单击和是否勾上没有必然的联系。可能你在点击Cell方框后,CheckBox的Value(是否勾上)不会发生变化(点在Cell中空白处)。

        这种情况,我一般将CheckBox的Value值通过程序进行控制。不依赖于Visual Studio 2005自动完成。具体处理步骤如下:

       1. 将DataGridViewCheckBoxColumn的ReadOnly属性设置为true。

       2. 添加DataGridView的CellClick方法。

            private void dgvRet_CellClick(object sender, DataGridViewCellEventArgs e)
            {
               

                if (this.dgvRet.CurrentCell.ColumnIndex == 0)
                {
                    //获取DataGridView中CheckBox的Cell
                    DataGridViewCheckBoxCell dgvCheck = (DataGridViewCheckBoxCell)(this.dgvRet.Rows[this.dgvRet.CurrentCell.RowIndex].Cells[0]);

                    //获取被选中列的相关信息

                    Double dAdd = double.Parse(this.dgvRet.Rows[this.dgvRet.CurrentCell.RowIndex].Cells[5].Value.ToString());

                    ...

                   //根据单击时,Cell的值进行处理。EditedFormattedValue和Value均可以

                   //若单击时,CheckBox没有被勾上
                    if (Convert.ToBoolean(dgvCheck.EditedFormattedValue) == false)
                    {
                        this.dTotal += dAdd;

                        ...   

                        //通过程序完成CheckBox是否勾上的控制
                        dgvCheck.Value = true;
                    }

                   //若单击时,CheckBox已经被勾上
                    else
                    {
                        this.dTotal -= dAdd;

                        ...   

                        //通过程序完成CheckBox是否勾上的控制
                        dgvCheck.Value = false;
                    }
                  
                    this.txtTotal.Text = this.dTotal.ToString();

                }
            }

      文章2   C# winfrom 中datagridview中checkbox的使用方法(转)

    http://blog.sina.com.cn/s/blog_4e51b5530100j5yz.html

    方法一:
    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来获取属性值

  • 相关阅读:
    HDU-1215 七夕节 数论 唯一分解定理 求约数之和
    LightOJ-1259 Goldbach`s Conjecture 数论 素数筛
    [前端-动态数据可视化]横向柱状图的动态数据可视化
    CodeForces-722C Destroying Array 并查集 离线操作
    CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
    CodeForces-1007A Reorder the Array 贪心 田忌赛马
    POJ-3692 Kindergarten 二分图 最大团
    个人开发者做一款Android App需要知道的事情
    android各种组件的监听器
    留言处插入xss不弹框
  • 原文地址:https://www.cnblogs.com/flylong0204/p/3921344.html
Copyright © 2011-2022 走看看