Link to home
Start Free TrialLog in
Avatar of Rowdyone52
Rowdyone52

asked on

Checkbox count DataGridView

I need to count the number of checkboxes that are checked in a datagridview control.  so basically column 0 is a checkbox field, and if someone checks a box I need to show 1 box checked in a label control.  How can I accomplish this?
Avatar of Neotk
Neotk
Flag of Brazil image

Yoo man!
you can do this:

in your checkbox inside the datagridview, you must set the event to increment your label count if is true
or decrement if it's false;

checkbox1.CheckedChanged = checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
  //If the checkbox is true increment else decrement.
   LabelCount.Text = ((CheckBox)sender).Checked ? (Convert.ToInt32(LabelCount.Text) + 1).ToString() :
                                                             (Convert.ToInt32(LabelCount.Text) - 1).ToString() :
}

Regards
Rodrigo Leote
Avatar of Rowdyone52
Rowdyone52

ASKER

How do I access the checkbox inside of the datagridview??
I access the checkboxes value by using
datagridview1[0,ROW].value

What event lets me know that the checkbox value has changed so that I may recalculate?
sorry, i forgot to add this.

//This will find the control inside tthe datagridview
CheckBox chkBox =  (CheckBox)dataGridView1.Controls["checkbox1"];

//This will set the event in the CheckBox
chkBox.CheckedChanged = checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

Ok im still a little lost.  Where in the program do I define chkBox?  

Does (CheckBox)dataGridView1.Controls["checkbox1"] give me access when the checkbox is clicked on any row?

ASKER CERTIFIED SOLUTION
Avatar of Neotk
Neotk
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial