Bluehost - Affordable, Reliable Web Hosting Solutions
Powered by MaxBlogPress 

Would you like to get paid for signing up for a web hosting plan with Bluehost.com?
Powered by MaxBlogPress 

Adding CheckBox in GridView

Posted on September 25, 2008
Filed Under Programming, microsoft | Leave a 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

Bookmark and Share
Sphere: Related Content

Other Related Posts:

  • Export GridView Data to MS-Excel
  • Beginning Ajax with Visual Studio 2005
  • Merging Datasets
  • News for Gmail sellers
  • Building Web Parts for SharePoint

  • If you've enjoyed reading this post then why not subscribe to received updates by email.

    Enter your email address:

    Delivered by FeedBurner

    Email This Post Email This Post

    Trackback This Post

    Comments

    Leave a Reply




    CommentLuv Enabled

    Twitter

    Posting tweet...

    Powered by Twitter Tools.