在DataGridView上查看所有复选框项目



这是场景。

我有checkbox(名称:"检查所有" ID:CHKITEMS)和datagridview。当我单击此复选框时,还将检查datagridview上的所有复选框。

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

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

这是复选框背后的代码。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;
        }
    }
}   
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell) row.Cells[0];

而不是

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
}
    private void setCheckBoxInDataGrid(DataGridView dgv, int pos, bool isChecked)
    {
        for (int i = 0; i < dgv.RowCount; i++)
        {
            dgv.Rows[i].DataGridView[pos, i].Value = isChecked;
        }
    }

这就是我的做法

尝试这个

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

如果您可以自行提供默认状态为 checkboxes of datagridview,即true或false [不分配零状态]状态(这样做的原因将在此中解释后者)。

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

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

其中DataGridView[0, i]指示0列ITH行
这样做的原因是,在加载时,复选框默认情况下是在null状态下。该代码没有比较null状态(创建对象null引用异常)。因此,一旦您将其分配为false或true。它永远无法进入null状态。
使用要检查

的Button_Click_event中输入以下代码
for (int i = 0; i < dgv.RowCount-1; i++)
{
    if (dgv.Rows[i].Cells[0].Value.ToString() != "")
    {
        dgv.Rows[i].Cells[0].Value = false;
    }
    else
    {
        dgv.Rows[i].Cells[0].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();

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);
}

这是我的版本,它允许我说的更自然的行为;如果一个复选框被打勾,则选择全部时也会勾选所有复选框。

如何对您有用:)

private void BtnSelectAll_Click(object sender, EventArgs e)
    {
        List<Boolean> chkList = new List<Boolean>();
        bool ticked = false;
    
        foreach (DataGridViewRow row in dataGrid.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            chkList.Add((bool)chk.Value);
        }
    
        if (!chkList.Contains(true))
        {
            ticked = true;
        }
        else if (!chkList.Contains(false))
        {
            ticked = false;
        } else
        {
            ticked = true;
        }
    
        foreach (DataGridViewRow row in dataGrid.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            chk.Value = ticked;
        }
    }

最新更新