[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1701
  • Last Modified:

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?
0
Rowdyone52
Asked:
Rowdyone52
  • 3
  • 3
1 Solution
 
NeotkCommented:
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
0
 
Rowdyone52Author Commented:
How do I access the checkbox inside of the datagridview??
0
 
Rowdyone52Author Commented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
NeotkCommented:
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);

0
 
Rowdyone52Author Commented:
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?

0
 
NeotkCommented:
Look, now i'm not at work, i can explain it better.

when you use this command: (CheckBox)dataGridView1.Controls["checkbox1"],
you are searching the control that is inside the dataGridView1 (this is the name example for your datagridview, but if you changed the default name, you have to use the datagridview instance name that you are using) and the name is "checkbox1", here if your checkbox have a different name you should put your checkbox name here. the "(CheckBox)" is just doing the cast of the Control to type "CheckBox.

the CheckBox chkBox is refering to the just found checkbox inside the datagridview.

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

this will set the event to your checkbox inside your datagridview. So, everytime you click it, it'll fire the event that sets in the label the counter.

you must define the chkBox in your Load wherever.

Sorry because its a little hard to explain.

Regards
Rodrigo Leote.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now