Adding CheckBox in GridView
Posted on September 25, 2008
Filed Under Programming, microsoft | 1 Comment
| If you've enjoyed reading this post then please subscribe to my Full Text RSS Feed. |
Recently I have been asked to knock up a front-end web page that will enable users to select multiple rows of data and update the data with one click. I decided to use the GridView control. The GridView control derives from System.Web.UI.WebControl.GridView class.
How to add a CheckBox control in a GridView using Visual Studio 2005.
What we want to achieve is to include checkboxes in the GridView and add two buttons to allow Select All/Unselect All
To add checkbox in the first column of the GridView in Visual Studio, click on Edit Templates in the GridView tasks. Then drag the CheckBox control from the Toolbox in Visual Studio into the ItemTemplate box as shown below.
Build your code and it should build successfully.
How to allow user Check/Uncheck all check boxes in the GridView
Add the following code to the button event. Button4_Click is for Check All and Button5_Click is for UnCheck All.
What the code does:
The line foreach(GridViewRow gv in GridView1.Rows) will iterate through each row in the GridView.
CheckBox cb = (CheckBox) gv.FindControl(”CheckBox1″) finds the control in the row, cast and assign to a CheckBox called cb.
cb.Checked = true
What this line of code does is it checks the CheckBox.
By iterating through the GridView, all the Checkboxes will be checked.
To UnCheck All, the only line of code that changes is cb.Checked = false
Sphere: Related ContentOther Related Posts:
If you've enjoyed reading this post then why not subscribe to received updates by email.
Email This Post
Trackback This Post
Comments
One Response to “Adding CheckBox in GridView”
Leave a Reply












Hi i like the post. You can further add the dynamic loading by adding a checkbox column. so it will be much customizable and re-usable.