Home > Database > Mysql Tutorial > body text

C# winfrom 中datagridview中checkbox的使用方法_MySQL

WBOY
Release: 2016-06-01 13:51:37
Original
1011 people have browsed it

GridViewDataGridCheckBox

方法一:
private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
            int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
            for (int i = 0; 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             {
                //如果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来获取属性值

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!