pharaon450
pharaon450

Reputation: 523

c# Is there an event for datagridview's checkedbox

i would like to know if there is an event for every time someone checks an datagridview's checkbox.

My goal is to count how many rows are checked but i want the count to be refreshed every time the user checks, so that is why i'm am wondering if there is an event for each check the user you do. (Just like in the normal checkbox, checkBox_CheckedChanged)

Thank you

Upvotes: 0

Views: 305

Answers (2)

batressc
batressc

Reputation: 1588

Try this:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
    //Closing current edited cell (for prevent problems)
    this.dataGridView1.EndEdit();
    //Retrive the datasource from the gridView in a DataTable (in this case, i use a DataSource with Visual Studio Wizard
    DataTable source = ((DataSet)((BindingSource)this.dataGridView1.DataSource).DataSource).Tables[0];
    //The magic code line ("IdCierre is my checkeable field" in the grid). I use Linq
    this.label_contador.Text = source.AsEnumerable().Where(x => x.Field<bool>("IdCierre") == true).Count().ToString();
}

Upvotes: 0

Samy Arous
Samy Arous

Reputation: 6814

No there isn't (as far as I know), but you can use this simple workaround:

private void dgAreas_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    IsChecked = (bool)dgAreas[e.ColumnIndex, e.RowIndex].EditedFormattedValue
    ...

}

You have to listen to the CellContentClick event.

Upvotes: 2

Related Questions