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;
    }
4
  • when these checkboxes are getting checked? just when the datagridview is bound with data? Commented Apr 7, 2015 at 3:15
  • Have a look at the following @Michay. stackoverflow.com/questions/1237829/… and also codeproject.com/Articles/42437/… Commented Apr 7, 2015 at 3: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? Commented Apr 7, 2015 at 3:17
  • @Grant Winney: I tried moving the num outside the event but still doesn't give the correct count. . . Commented Apr 7, 2015 at 3:28

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

As suggested by @titol in a proposed edit, consider checking for (e.RowIndex < 0) as shown in Index out of range exception in datagridview when header is selected.
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;
    }

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.