How to work with checkboxes in gridview ASP.NET?

No.of Views1493
Bookmarked0 times
Downloads 
Votes0
By  Pankaj Kumar Gupta   On  14 Feb 2011 03:02:44
Tag : ASP.NET , Grid Controls
This CodeSnippet can be used to add and handle the checkboxes in GridView
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Add the below code for adding the check boxes in your GridView.This is very simple snippet at the same time.very useful.

Html Code

<asp:TemplateField HeaderText="Select">
<ItemStyle HorizontalAlign="Center" Width="5%" />
<HeaderStyle width="5%" HorizontalAlign="Center"/>
<ItemTemplate >
<asp:CheckBox Enabled="true" ID="chkBox" runat="server"/>
</ItemTemplate>
</asp:TemplateField>

Then Write the below code in button click event

C# Code

foreach (GridViewRow row in gridview.Rows)
{
  CheckBox CheckBox1 = (CheckBox)row.FindControl("chkBox");
  //Checking for the checkbox is checked or not
  if (CheckBox1.Checked == true)
  { 
    ID= gridview.DataKeys[row.RowIndex].Value.ToString();
  }
}

Hopes help and Thank you.

 
Sign Up to vote for this article
 
About Author
 
Pankaj Kumar Gupta
Occupation-Software Engineer
Company-Miri Infotech (P) Ltd, India
Member Type-Junior
Location-India
Joined date-27 Oct 2010
Home Page-www.codegain.com
Blog Page-codegain.com
- I am working with Miri Infotech (P) Ltd, India as Senior Software Engineer. - Having 5+ years experience in .NET Technologies (ASP.NET, C#.net, VB.net, SQL Server, XML, Web Services, MS Dynamics CRM, SharePoint). - I have masters M.Sc-Computer Science, M.Tech-Information Technology with Honours. - I have completed MCP, MCSD.NET, MCAD, MCDBA SQL Server, MCTS, MCPD-EAD - Microsoft Certified Professional & Technology Specialist - Obsessed in OOP, MVC, MVP style design and programming. - Designing and developing the client/server applications for a number of doimains. - Designing and implementing Business Planning Tools & Applications. - Good understanding of formal software engineering tools & technologies.
 
 
Other popularSectionarticles
    this codesnippet is very tricky because it shows a trick about exporting datalist data to excel.
    Published Date : 09/Jun/2010
    In many situations we may need to add auto number or serial numbers in grid view column, but we cannot find a property in property window to add this.
    Published Date : 26/May/2010
    In this article I am going to give you a brief explanation regarding how to use a Radio button in grid view. Actually I am going to show how we can delete a row from grid view by selecting the radio button inside the grid view and by clicking on delete button outside the grid view
    Published Date : 15/May/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top