检查DataGridView上的所有复选框项 [英] Check All Checkbox items on DataGridView

查看:127
本文介绍了检查DataGridView上的所有复选框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是场景,



我有复选框(名称:检查所有ID:chkItems)和datagridview。当我点击复选框。



我也在网格上添加了复选框列。

  DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn(); 
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
GridView1.Columns.Add(CheckboxColumn);

这是复选框背后的代码。在行上有问题。

  private void chkItems_CheckedChanged(object sender,EventArgs e)
{
foreach(GridView1.Rows中的DataGridViewRow行)
{
DataGridViewCheckBoxCell chk = e.row.Cells(0);
if(chk.Selected == false)
{
row.Cells(0).Value = true;
}
}
}

SOLVED )

  private void chkItems_CheckedChanged(object sender,EventArgs e)
{
foreach(GridView1中的DataGridViewRow行.Rows)
{
DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells [1];
if(chk.Selected == false)
{
chk.Selected = true;
}
}
}


解决方案>

  DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells [0]; 

而不是

  DataGridViewCheckBoxCell chk = e.row.Cell(0); 

* EDIT: *我想你真的想这样做: / p>

  foreach(dataGridView1.Rows中的DataGridViewRow行)
{
DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells [0];
chk.Value =!(chk.Value == null?false:(bool)chk.Value); //因为chk.Value是initialy null
}


here's the scenario,

i have checkbox(Name:Check All ID:chkItems) and datagridview. And when i click on the checkbox. All checkbox on the gridview will also be checked.

I've also added the checkbox column on the grid.

DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            GridView1.Columns.Add(CheckboxColumn);

Here is the code behind of the checkbox. there is a problem on the row.Cell

private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in GridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = e.row.Cells(0);
                if (chk.Selected == false)
                {
                    row.Cells(0).Value = true;
                }
            }
        }   

SOLVED (here is the solution)

private void chkItems_CheckedChanged(object sender, EventArgs e)
        {   
            foreach (DataGridViewRow row in GridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
                if (chk.Selected == false)
                {
                    chk.Selected = true;
                }
            } 
        }   

解决方案

DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];

instead of

DataGridViewCheckBoxCell chk = e.row.Cell(0);

*EDIT:*I think you really want to do this:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
       DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];
       chk.Value = !(chk.Value == null ? false : (bool) chk.Value); //because chk.Value is initialy null
}

这篇关于检查DataGridView上的所有复选框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆