DataGrid and Checkboxes
page 1 of 1
Published: 09 May 2002
Unedited - Community Contributed
Abstract
Working with Checkboxes and Datagrid is an interesting aspect. Their may be many situations in which, we need to allow the user to select many rows in a table or datagrid. Think of a datagrid which has a checkbox in the first column. User can select any number of rows they may need. Our job is to find the number of rows selected by the user. Once we have the ID of the selected rows, we can do anything with those rows. We can delete or what ever we need to do with the selected rows. In this article, we will see, how to know the number of rows selected by the user. We will have a Datagrid and a button. While the user clicks on the button, we will parse through the datagrid and find the rows selected by the user.
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 65950/ 52

DataGrid and Checkboxes

Written on: May, 9th 2002.
Introduction

Working with Checkboxes and Datagrid is an interesting aspect. Their may be many situations in which, we need to allow the user to select many rows in a table or datagrid. Think of a datagrid which has a checkbox in the first column. User can select any number of rows they may need. Our job is to find the number of rows selected by the user. Once we have the ID of the selected rows, we can do anything with those rows. We can delete or what ever we need to do with the selected rows. In this article, we will see, how to know the number of rows selected by the user. We will have a Datagrid and a button. While the user clicks on the button, we will parse through the datagrid and find the rows selected by the user.

I assume that, the reader knows to create a simple datagrid with TemplateColumn and ItemTemplate tags.

Aspects that we will be learn in this article
  1. How to populate a DataGrid?
  2. How to parse through the rows and columns in a Datagrid?
  3. How to find the value of a checkbox?
  4. How to retrieve all the information of a selected row?

Populating the DataGrid

For our example, we will take the table STORE in the PUBS Database. Since stored procedures are very much better than inline query, we use a stored procedure called, sp_stores_sel, which contains a single SQL statement. The SQL statement would be Select * from stores.

How to parse through all the rows and columns in a DataGrid?

In the introduction, we saw that we need a button to select the rows. The code that we are going to discuss is in the OnClick event of the Select button.

The primary thing that we need is a DataGridItem. Then we need to make use of the FindControl method to retrieve the value of checkbox and other columns in the datagrid. Let us take a look at the code within the OnClick event of the button.

Code in the OnClick event of the Select button.
    Dim myDataGridItem As DataGridItem
    Dim chkSelected As System.Web.UI.WebControls.CheckBox
    Dim strStoreName As String
    Dim strStoreID As String

    lblStatus.Text = "<br>You selected the Following items:<br><br>"

    For Each myDataGridItem In DG_CheckBox.Items

        chkSelected = myDataGridItem.FindControl("chkSelection")
        If chkSelected.Checked Then
            strStoreName = CType(myDataGridItem.FindControl("lblStoreName"), Label).Text
            strStoreID = CType(myDataGridItem.FindControl("hdnStoreID"), Label).Text
            lblStatus.Text += "The store name is <b>" & strStoreName & "</b>"
            lblStatus.Text += " and the StoreID is <b>" & strStoreID & "</b><br>"
        End If

    Next

How it works?

In the above code, we have four variables. An instance of DataGridItem, a CheckBox, and two String variables. We need a looping structure to parse through all the rows and columns. The best one is the For loop. Since we have a DataGridItem, we need to use this in the For Loop.

The statement For Each myDataGridItem In DG_CheckBox.Items starts a loop which parses through all the rows using the property Items of the datagrid. DG_CheckBox is the name of the DataGrid.

In a datagrid, we will have many columns. We need to retrieve the value of checkbox in each row and the StoreID for that row. The best way to retrieve the value of each column is by using the FindControl method. The name of the CheckBox control is chkSelection. So, we should have a FindControl method which returns the CheckBox object back and this is done by chkSelected = myDataGridItem.FindControl("chkSelection"). The above statement is the most important statement in this article. It is the above statement which retrieves the value of each checkbox. Once we have the value of each checkbox, then all you have do is

# Check whether the checkbox is checked or not, by using the property, Checked.
# If the Checkbox is Checked, then use the FindControl method to retrieve other fields such as StoreID and StoreName.

Sample output of our scenario

DataGrid with Checkboxes - 35,452 bytes
Fig: DataGrid with Checkboxes.

Test this Script

Download the code

Click here to download the ASPX page
Click here to download the Stored Procedure

Conclusion

Checkboxes in a Datagrid is a very useful one in most of the web applications. Just use this code when ever you want to select a group of items from a DataGrid.

Links

How to Edit a DataGrid?
How to capture a Double Click event in DataGrid?
Checkbox Web Server Control
Retrieving Images from SqlServer in ASP .NET
Retrieving Images from SqlServer and displaying it in DataGrid

Send your comments to das@aspalliance.com        Back to Article list



User Comments

Title: sql   
Name: date
Date: 2011-12-18 4:02:53 AM
Comment:
"how to insert date dd-mm-yy & 2:30 pm in seperate coloumn using assembly?" plz send me ans in my mailid
anasam68@gamil.com
Title: DataGrid   
Name: Pravin Dharammali
Date: 2011-03-03 12:21:01 AM
Comment:
"How to popup dropdownlist in datagrid at runtime."
Your Above article is really nice for lerner in asp.net regarding datagrid problem.

if you have my sol'n then please send me on my MailId.

pravinmh31@gmail.com
Title: DataGrid   
Name: Bijay Kumar
Date: 2011-03-03 12:12:17 AM
Comment:
actually i need , when we load data in datagrid then that time checkbox automatically will check if that rows/data are already present in database.
But above article is vary nice for learning.
if you have a sol'n please send me in my mailId.

b_bijaykumar@sify.com
Title: Datagrid   
Name: Andy Dominguez
Date: 2010-07-02 10:52:37 AM
Comment:
This is a great turorial. I do have one question say us have users that wants a button to check all the checkboxes or uncheck them if they are already selected. how would you go about doing that?
Title: Datagrid   
Name: Mallarapu.Sreekanth
Date: 2010-05-29 4:32:47 AM
Comment:
HTTP Error 503. The service is unavailable.please check, datagrid useful
Title: update the table when checked the checkbox in datagrid   
Name: Biltu Khawas
Date: 2010-03-08 5:24:14 AM
Comment:
Hi,
i'm using datagrid and i want to update the table while i checked the checkbox.
What is the answer for this problem ?

biltu.khawas@gmail.com
Title: good   
Name: rajin
Date: 2010-02-02 5:18:34 AM
Comment:
good
Title: how to edit,update and cancel the data in a Datagrid with out using any controls..the operation will be performed by only the Datagrid   
Name: R.Muralidharan
Date: 2009-10-27 11:20:33 AM
Comment:
hai,
how will be do the following operation of edit,update and cancel in a datagrid without using any of the controls..pls reply me and send the demos and source code in both VB.NET and C# to my mail id...

thank you...have a nice day.,

by,

nrmuralidharan@gmail.com
Title: How can add ListBox in DataGridView   
Name: Chetan
Date: 2009-05-05 1:40:31 AM
Comment:
How can i Add ListBox in DataGridView?


Reply
this Email ID
chetanchaudhari03@yahoo.com
Plz this is very imp for me.............
Title: how to add checkboxes in datagridview & how to handle the event of checkbox   
Name: Kets
Date: 2008-12-31 6:34:30 AM
Comment:
how can we add checkboxes in datagridview & how can handles the event of checkbox
Title: how to get Datagrid particular row   
Name: Rams
Date: 2008-12-07 7:22:41 AM
Comment:
how to get only checkbox checked datagrid items .why looping every datgrid rows?
Title: DataGrid and Checkboxes   
Name: Gragi
Date: 2008-08-07 12:40:14 AM
Comment:
Can I get the above code in C#. I really appreciate your help.
Thank you, Gragi
Title: DataGrid and Checkboxes   
Name: mark
Date: 2008-07-28 10:28:41 PM
Comment:
nice. it helps a lot thanks
Title: add Paging Error   
Name: pujan
Date: 2008-06-03 5:00:01 AM
Comment:
hi,
thx for posted this article.
but i have problem when i add paging to the grid
could u help me pls..
Title: Fresher   
Name: Paulomi
Date: 2008-04-17 7:13:57 AM
Comment:
Hi

I have a checkbox column in the datagridview...the grid is bound to an Access database table...now i want to color those rows where the checkbox is checked in a button click event....its a windows application please help...need the code in VisualC#....Thanks
Title: update delete & add records code   
Name: pankaj
Date: 2008-02-21 2:03:48 AM
Comment:
i need code for these in datagrid using asp.net with c# code. ....thank you.
Title: Thank you   
Name: Divya
Date: 2008-02-08 12:00:48 PM
Comment:
I found it very helpful,thank you
Title: update delete & add records code   
Name: Rama Krishna D
Date: 2008-02-04 12:05:55 AM
Comment:
i need a code of add,update delete in datagrid using asp.net with C# code.... can u send very urgent........... thank u
Title: All controls of asp.net 2.0   
Name: seenu
Date: 2008-01-30 3:37:04 AM
Comment:
hi,,
i want to know about all controls of asp.net 2.0 in c#.net......pls send this information is send to my mail...my mail id is anjali_satheesan@yahoo.co.in.....pls send urgently.........
Title: Mr   
Name: Goodman
Date: 2008-01-18 5:39:23 AM
Comment:
Hi

i got your email address from VB.NET help function of Visual Studio 2003.I have a datagrid in it i have a column "amount".I created textfield in VB.NET which is "total amount" offwhich i want the sum of the amounts in "amount field to be populated in "total amount" textfield i'm using oracle 10g database.I need this info urgently if you can help asap that would be highly appreciated.Please send me your answer to g_dayile@yahoo.com

looking forward hearing from you

Regards
Goodman
Title: Team Lead   
Name: Faisal Shah
Date: 2007-11-27 1:16:54 AM
Comment:
its a fantastic article.... thanks for helping me... thank you so much
Title: Check whether checkbox is checked or not   
Name: Banty
Date: 2007-11-20 6:11:15 AM
Comment:
It is very useful in many applications such as mail , where we select check boxes beside tyhe mails and say delete they get deleted or to send mail to selected users.
Title: choose checkbox   
Name: Askar
Date: 2007-10-23 1:57:25 AM
Comment:
can any one tell me the number of check box in list item, How can choose only one item in this list item, multiple check not allowed
Title: sumum   
Name: minjo
Date: 2007-05-23 12:50:42 PM
Comment:
i have a two tables, one is for iterms and the other one is for customers, the iterm table have prices.the customer select lot of iterms and he wants to know the price. can some tell me how to add up the sum of all the iterms selected by one customers using sql.
Title: How to enable and disable the checkbox in a datagrid using datagridboolcolumn()   
Name: vichu
Date: 2007-05-03 7:28:47 AM
Comment:
Can anyone tell me how to enable or disable the checkbox in a datagrid by using the datagridboolcolumn() method. The output i get is grayed......
Title: Datagrid   
Name: moorthy
Date: 2007-04-25 8:09:09 AM
Comment:
i need a code of printout from selected chechboxes in datagrid using asp.net with vb code.... can u send very urgent........... thank u
Title: To find the whether checkbox is checked or not   
Name: sangeetha
Date: 2007-04-18 5:46:59 AM
Comment:
very good. it was very helpful.
Title: DataGrid and Checkboxes   
Name: Giri
Date: 2007-04-09 4:50:25 AM
Comment:
It is useful in many applications such as mail , where we select check boxes beside tyhe mails and say delete they get deleted.
Title: using checkbox in datagrid   
Name: leena
Date: 2007-03-30 11:25:53 AM
Comment:
article is excellent
coding is simple and easy
but c# should also be there
Title: How to change the row color in datagrid   
Name: Vijay
Date: 2007-03-17 6:50:08 AM
Comment:
hi
do u know how ot change the row color or row width in vb.net smartdevice datagrid
Title: how to delete using checkbox   
Name: balaji
Date: 2007-03-15 1:51:58 AM
Comment:
hi
guys do u know how to delete
the particular item in datagrid using checkbox
Title: Superb!   
Name: Roja banu M.
Date: 2007-02-26 5:30:48 AM
Comment:
you have done a good job for us.Thank you!.can you help me to convert them into c# codings....
Title: Finding selected rows in data grid   
Name: Pushpendra
Date: 2007-02-15 9:20:25 AM
Comment:
This is the best and faster solution i have ever found.
Title: good article   
Name: babu
Date: 2007-01-30 5:49:59 AM
Comment:
this is a good site.
Title: Very Helpful article to newcomers in .Net   
Name: Prashant
Date: 2007-01-11 3:57:29 AM
Comment:
I m very Happy with this article......Thanks a lot.
Title: check box   
Name: jai
Date: 2006-12-21 11:49:27 PM
Comment:
i will check box add in datagrid at run time.
after how will get check id ,value?
tell me
Title: This is Good PRogramming   
Name: Venkata Ramana Peddyreddy
Date: 2006-12-20 4:42:51 AM
Comment:
this is venkata ramana
i would like to explain my feelings soon
Title: Good one   
Name: Ancilla
Date: 2006-11-08 2:54:48 AM
Comment:
Like this only i want to delete each row using heckbox not in vb.net in c#
Title: nice article   
Name: tirmal Alex
Date: 2006-10-16 12:55:36 AM
Comment:
it's nice das.
Title: super   
Name: student
Date: 2006-09-27 3:12:00 AM
Comment:
I expect more like this i have learnt lot from this site
Title: gopalan   
Name: gopalan
Date: 2006-09-23 4:30:37 AM
Comment:
good one.........
Title: wow   
Name: WAQAS BUTT
Date: 2006-09-05 3:49:23 AM
Comment:
THANX MR. DAS

I GOT THE REQUIRED THING FOR COMPLETION OF MY PROJECT
THANX AGAIN
Title: grid is a nice 1   
Name: saravanan
Date: 2006-08-08 2:58:48 AM
Comment:
i don,t read the above article but it was nice one
Title: Best One   
Name: shajee ali
Date: 2006-08-07 7:40:31 AM
Comment:
good help for all developers thanks buddy
Title: Lal   
Name: Abhinav
Date: 2006-07-24 6:10:08 PM
Comment:
I do not have better words other than Thanks and Thanks alot.
Title: very helpful guide for webdevelopers   
Name: raja saha
Date: 2006-07-15 8:40:23 AM
Comment:
the site is very good for webdevelopers who develops and updates a running project each and every time and upload it.
Title: DGA   
Name: saravanan
Date: 2006-07-11 12:59:24 AM
Comment:
hai das
it is a good article
keep it up........
thank u
Title: how to maintain checkbox in a datagrid?   
Name: Nandu
Date: 2006-06-28 9:09:17 AM
Comment:
Good!

How to maintain the checkbox levels in a datagrid for different page(index) level. (if it is c# well and good)

Thanks
Title: data grid   
Name: samim mondal
Date: 2006-06-09 4:07:30 AM
Comment:
very very good
Title: Gud Article   
Name: Irsh
Date: 2006-04-17 1:47:16 AM
Comment:
Nice one..but can anyone send it to me done in C#.
Title: i can't make it work   
Name: Eddie
Date: 2006-04-05 1:23:47 PM
Comment:
I converted this code to c# but I just can't make it work. It seems that the click event of the button causes a postback and resets the checkboxes to their original value before the code behind this event is executed. Since the default value is unchecked, the code just runs thru the datagriditems collection without doing anything. Please help.
Title: its help me alot   
Name: ashutosh kumar pandey
Date: 2006-03-31 1:43:07 PM
Comment:
very very good
Title: Good   
Name: kisuva
Date: 2006-03-17 5:09:50 AM
Comment:
I same that sathya i need it in c#
Title: Amazing   
Name: Bala
Date: 2006-01-27 10:49:32 AM
Comment:
Its really good for the begginers.Thanks a lot
Title: heh   
Name: csover
Date: 2005-11-12 10:23:51 AM
Comment:
i think i may be dig into.but thanks so much.
Title: Excellent Job   
Name: Mohit
Date: 2005-08-10 6:54:00 AM
Comment:
This is a very good article. It helped me a lot in understanding the concept of referring to checkboxes. Can you write some more examples with respect to nested controls lets say Datalist control within which there is one datagrid control within which check box , images and a button which is once clicked can refer to all checkboxes.
Title: Really It is a good article   
Name: ashok kumar
Date: 2005-07-25 4:54:37 AM
Comment:
Hai Das Really this is very good article

bye
ashok kumar
Title: trying to get this to work   
Name: Ryan
Date: 2005-07-01 8:50:07 AM
Comment:
i am trying to get this to work in my c# code in visual studio 2003. when i call DataGrid1.FindControl("CheckBox1"); it says i cant implicitly convert System.Web.UI.Control to System.Web.UI.WebControls.CheckBox. is it possible there is a conversion im missing, my datagrid consits of 9 colums the first one is the only one without a checkbox. can someone suggest some ideas
Title: OH ITS REALLY NICE IT HELPED IN MY PROJECT   
Name: P.GOPI KRISHNA
Date: 2005-06-23 4:56:00 AM
Comment:
WHY DONT U PUBLISH SOME MORE ARTICLES
Title: Great !   
Name: Jean-Luc
Date: 2005-05-05 9:17:21 PM
Comment:
Well done Das !
Good article, working just fine !
Title: Very Nice Article   
Name: Avdhesh
Date: 2005-03-24 12:36:11 AM
Comment:
THis is really Good article
Title: 10x   
Name: Muhammed Aslam
Date: 2005-03-18 1:08:58 AM
Comment:
WOW GR8 CODE...
Its Helped my project thanx
but im C# developer, I converted this
thnx
Title: TO Sathya   
Name: overred
Date: 2005-03-16 8:59:50 AM
Comment:
C# public can with others tools
Title: Good one   
Name: Sathya
Date: 2005-03-14 4:30:25 AM
Comment:
Hi das,this is a good work.But I need it in C# codings.

bye
Sathya
Title: nice article   
Name: Hazree
Date: 2005-02-17 11:22:35 PM
Comment:
Its really works. I made some code modification, so i can delete multiple record using checkbox.
Title: hell yeah..   
Name: Jusman Jawal
Date: 2005-02-15 2:20:21 AM
Comment:
good article and put some more laa. it helps people all around the world on programming. - welcome to malaysia.
Title: Excellent Job   
Name: C#.NET fan
Date: 2004-10-17 6:40:39 PM
Comment:
Hi Das,

Good Job
Title: good article   
Name: unknown
Date: 2004-09-29 8:35:00 AM
Comment:
this is a good site.
try to add some more good articles






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-09-25 5:47:45 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search