Check All Checkbox items on DataGridView

吃可爱长大的小学妹 提交于 2019-12-22 08:28:08

问题


Here's the scenario.

I have checkbox(Name:"Check All" ID:chkItems) and datagridview. And when I click on this checkbox, all checkboxes on the datagridview 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;
        }
    } 
}   

回答1:


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
}



回答2:


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

This is how I did it




回答3:


Try this one

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

}



回答4:


If you are okay with providing a default state to the checkboxes of datagridview on your own i.e either True or False[Do not assign a null state] state(Reason for doing this would be explained in the latter).

Which could be done by the following code,(type in this code when you search for results to be viewed in DataGridView)
dgv is the object of the DataGridView that you are using.

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

Where DataGridView[0, i] indicates 0th column ith row
The Reason for doing this is,On load the checkbox is by default in a null state. The code isn't comparing for null state(Creating a object null reference exception). So, once when u assign it a state either a false or true . It can never undergo into null state.
Type in the following code inside the button_click_event using which you are going to check

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

It Worked for me, I hope it does for you.




回答5:


I tried to select all checkbox or select it mutuality and calculate some value...so wrote this code that's maybe helpful.

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


来源:https://stackoverflow.com/questions/13242861/check-all-checkbox-items-on-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!