C# 选中DataGridView上的所有复选框项

C# 选中DataGridView上的所有复选框项,c#,winforms,datagridview,checkbox,C#,Winforms,Datagridview,Checkbox,下面是一个场景 我有复选框(名称:“Check All”ID:chkItems)和datagridview。当我点击这个复选框时,datagridview上的所有复选框也将被选中 我还在网格上添加了复选框列 DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn(); CheckBox chk = new CheckBox(); CheckboxColumn.Width = 20; GridView1.

下面是一个场景

我有
复选框
(名称:“Check All”ID:chkItems)和
datagridview
。当我点击这个复选框时,
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 (DataGridViewRow row in GridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = e.row.Cells(0);
        if (chk.Selected == false)
        {
            row.Cells(0).Value = true;
        }
    }
}   
而不是

DataGridViewCheckBoxCell chk = e.row.Cell(0);
*编辑:*我想你真的想这样做:

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的
复选框提供默认状态,即True或False[不指定空状态]状态(后面将解释这样做的原因)

这可以通过以下代码完成(在
DataGridView
中搜索要查看的结果时键入此代码)
dgv
是您正在使用的
DataGridView
的对象

for (int i = 0; i < dgv.RowCount - 1; i++)
{
     dgv.Rows[i].DataGridView[0, i].Value = true;
}

它对我有用,我希望它对你有用。

我试图选中所有复选框或选择它的相互性并计算一些值……所以写了这段代码,可能会有所帮助

foreach (DataGridViewRow item in DGDoc.Rows)
{
    if (item.Cells[0].Value == null)
        item.Cells[0].Value = "True";
    if (bool.Parse(item.Cells[0].Value.ToString()))
    {
        item.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(241, 215, 215);
        strIDs += "," + item.Cells[1].Value.ToString();
        intSumPrice += Int64.Parse(item.Cells[4].Value.ToString());
        intSumTax += Int64.Parse(item.Cells[5].Value.ToString());
        intSumPay += Int64.Parse(item.Cells[6].Value.ToString());
    }
    else
    {
        item.DefaultCellStyle.BackColor = System.Drawing.Color.Empty;
    }
}
DGDoc.EndEdit();
private void setCheckBoxInDataGrid(DataGridView dgv、int pos、bool已检查)
{
对于(int i=0;i
我就是这样做的

试试这个

foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
      row.Cells[0].Value = row.Cells[0].Value == false ? true : false;

}
1-创建新按钮

2-单击checkAll按钮时,可以使用以下代码

3-单击按钮时,它将选中datagridview中的所有复选框,再次单击时,它将取消选中所有复选框

private void btncheckall_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgvResult.Rows)
            {
                row.Cells[0].Value = row.Cells[0].Value == null ? false : !(bool)row.Cells[0].Value;
            }
        }

注意:在某些情况下,您必须先在datagridview中单击,然后单击按钮。

您可以像这样检查所有单元格:

private void CheckAllCheckboxItemsOnDataGridView(int columnIndex)
{ 
    foreach (DataGridViewRow row in dgFiles.Rows)
    {
        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)row.Cells[columnIndex];

        cell.Value = !(cell.Value == null ? false : (bool)cell.Value); 
    }
}
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
    CheckAllCheckboxItemsOnDataGridView(columnIndex: 0);
}
您可以在CheckedChanged事件中使用以下方法:

private void CheckAllCheckboxItemsOnDataGridView(int columnIndex)
{ 
    foreach (DataGridViewRow row in dgFiles.Rows)
    {
        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)row.Cells[columnIndex];

        cell.Value = !(cell.Value == null ? false : (bool)cell.Value); 
    }
}
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
    CheckAllCheckboxItemsOnDataGridView(columnIndex: 0);
}

当我尝试这个方法时,DataGridViewCheckBoxCell chk=row.Cells[0];行单元格[0]上仍有错误;检查编辑,您需要将单元格强制转换为DataGridViewCheckBoxCell我创建的复选框列有错误//无法将类型为“System.Windows.Forms.DataGridViewImageCell”的对象强制转换为类型为“System.Windows.Forms.DataGridViewCheckBoxCell”。datagridview中DataGridViewCheckBoxColumn的索引是什么?我已添加了此列DataGridViewCheckBoxColumn CheckboxColumn=新建DataGridView CheckboxColumn();复选框chk=新复选框();CheckboxColumn.Width=20;GridView1.Columns.Add(CheckboxColumn);
private void chkItems_CheckedChanged(object sender, EventArgs e)
{
    CheckAllCheckboxItemsOnDataGridView(columnIndex: 0);
}