3

I was unable to count checked checkboxes in DataGridView. I want to count the checked checkboxes during the checkbox is checked and store the number of checked items in a label. I tried the following code but does not give the correct count:

    int num = 0;
    private void dgvLoadData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        bool isChecked = Convert.ToBoolean(dgvLoadData.Rows[dgvLoadData.CurrentCell.RowIndex].Cells[0].Value.ToString());

        if (isChecked)
        {
            num+=1;
        }
        else
        {
            num-=1;
        }

        labelSelectedSum.Text = "Selected Items: " + num;
    }
ThEpRoGrAmMiNgNoOb
  • 1,256
  • 3
  • 23
  • 46
  • when these checkboxes are getting checked? just when the datagridview is bound with data? – renakre Apr 07 '15 at 03:15
  • Have a look at the following @Michay. http://stackoverflow.com/questions/1237829/datagridview-checkbox-column-value-and-functionality and also http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat – James Shaw Apr 07 '15 at 03:15
  • 1
    your code appears to only work with a single row, returning `1` if the cell is checked, and `-1` if it is not. As you are only working with a single row, how do you expect to get a count of anything? – Sam Axe Apr 07 '15 at 03:17
  • @Grant Winney: I tried moving the num outside the event but still doesn't give the correct count. . . – ThEpRoGrAmMiNgNoOb Apr 07 '15 at 03:28

3 Answers3

4

Apply CurrentCellDirtyStateChanged event on the table. Call gridview.CommitEdit to update value of the checkbox column. Do the following:

private void dgvLoadData_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dgvLoadData.IsCurrentCellDirty)
    {
        dgvLoadData.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

That will call _CellValueChanged event. No changes will be done on the codes inside CellValueChanged event:

int num = 0;
private void dgvLoadData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0)
            return;
    bool isChecked = (bool)dgvItemsToShip.Rows[e.RowIndex].Cells[0].Value;

    if (isChecked)
    {
        num+=1;
    }
    else
    {
        num-=1;
    }

    labelSelectedSum.Text = "Selected Items: " + num;
}
titol
  • 999
  • 13
  • 25
ThEpRoGrAmMiNgNoOb
  • 1,256
  • 3
  • 23
  • 46
  • 3
    As suggested by @titol in a [proposed edit](https://stackoverflow.com/review/suggested-edits/22477784), consider checking for `(e.RowIndex < 0)` as shown in [Index out of range exception in datagridview when header is selected](https://stackoverflow.com/q/24945342). – dbc Mar 15 '19 at 17:35
0

You can use the event: CellContentClick and CellContentDoubleClick:

Good Luck!

int num = 0;
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        bool isChecked = (bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
        CheckCount(isChecked);
    }
    private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        bool isChecked = (bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
        CheckCount(isChecked);
    }
    private void CheckCount(bool isChecked)
    {
        if (isChecked)
        {
            num++;
        }
        else
        {
            num--;
        }
        labelSelectedSum.Text = "Selected Items: " + num;
    }
0

I have a DataTable bound to my DataGridView and I check if the first column has any checkbox checked Here is my example :

private void dataGridViewMain_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
  if (e.ColumnIndex == 0)
  {
    int numberOfRow = dataTableCsvFile.AsEnumerable().Count(r => r[0].ToString() == true.ToString());
    buttonDataGridviewVerify.Enabled = numberOfRow > 0;
  }
}
Fred Smith
  • 2,047
  • 3
  • 25
  • 36