Solved

Checkbox count DataGridView

Posted on 2007-03-29
6
1,686 Views
Last Modified: 2008-01-09
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
Comment
Question by:Rowdyone52
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:Neotk
ID: 18819522
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
 

Author Comment

by:Rowdyone52
ID: 18819654
How do I access the checkbox inside of the datagridview??
0
 

Author Comment

by:Rowdyone52
ID: 18819734
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:Neotk
ID: 18819765
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
 

Author Comment

by:Rowdyone52
ID: 18820040
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
 
LVL 4

Accepted Solution

by:
Neotk earned 500 total points
ID: 18821115
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now