Solved

Checkbox count DataGridView

Posted on 2007-03-29
6
1,695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

726 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