Get Row Cell Value From Grid View of Checked Checkbox in ASP.Net

This article explains how to get a row's cell value from a Grid View of a checked CheckBox in ASP.NET and C#. I have a GridView and inside it is a checkbox. Now I will explain how to get row values from a GridView when the checkbox is selected using ASP.NET and C#.

Procedure:

1. Now first create the Emp table in the database. 

  1. Create table tbl_Emp(EmpId int, EmpName varchar(50), Company varchar(50), Address varchar(500))  
2. And insert some records into the table. 
  1. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(1, 'Ram''IDSLogic''DemoAddress')  
  2. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(2, 'Shyam''HCl''DemoAddress')  
  3. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(3, 'jitendra''Wipro''DemoAddress')  
  4. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(4, 'Rihan''TCS''DemoAddress')  
  5. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(5, 'Rohan''Tech''DemoAddress')  
  6. Insert into tbl_Emp( EmpId, EmpName, Company, Address) values(6, 'Sohan''Newtech''DemoAddress')  
3. After creating the table create the application (File ---> New ---> Website). 

NewWebsite

4. Give the application the name “SelectedRowCellvalueGridview”.

EmptyWebsites

5. Then create a new item.

NewItem

WebForm

Now your aspx page is like this:

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title>Selected Row Cell value Gridview</title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.         <div>  
  8.             <asp:GridView ID="gvEmp" AutoGenerateColumns="false" CellPadding="5" runat="server">  
  9.                 <Columns>  
  10.                     <asp:TemplateField>  
  11.                         <HeaderTemplate>   
  12.                             <asp:CheckBox ID="chkAllSelect" runat="server" onclick="CheckAll(this);" />  
  13.                         </HeaderTemplate>  
  14.                         <ItemTemplate>  
  15.                             <asp:CheckBox ID="chkSelect" runat="server" />  
  16.                         </ItemTemplate>  
  17.                     </asp:TemplateField>  
  18.                     <asp:BoundField HeaderText="EmpId" DataField="EmpId" />  
  19.                     <asp:BoundField HeaderText="EmpName" DataField="EmpName" />  
  20.                     <asp:BoundField HeaderText="Company" DataField="Company" />  
  21.                     <asp:BoundField HeaderText="Address" DataField="Address" />  
  22.                 </Columns>  
  23.                 <HeaderStyle BackColor="#5d5d5d" Font-Bold="true" ForeColor="White" />  
  24.             </asp:GridView>  
  25.             <asp:Button ID="btnGetRecord" Text="Get Selected Records" runat="server"  
  26.                 Font-Bold="true" OnClick="btnGetRecord_Click" /><br /><br />  
  27.   
  28.               
  29.             <asp:Label ID="lblRecord" runat="server" />              
  30.         </div>  
  31.     </form>  
  32.   
  33.   
  34.     <script type="text/javascript">  
  35. // for check all checkbox  
  36.         function CheckAll(Checkbox) {  
  37.             var GridVwHeaderCheckbox = document.getElementById("<%=gvEmp.ClientID %>");  
  38.             for (i = 1; i < GridVwHeaderCheckbox.rows.length; i++) {  
  39.                 GridVwHeaderCheckbox.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;  
  40.             }  
  41.         }  
  42.     </script>  
  43. </body>  
  44. </html>  

Now add “using System.Data.SqlClient;” and “using System.Data;” in the code behind and write the following code to get the checked check box row values.

  1. protected void Page_Load(object sender, EventArgs e)    
  2. {    
  3.     if (!IsPostBack)    
  4.     {    
  5.         Bindgv();    
  6.     }    
  7. }    
  8. // For bind Gridview    
  9. protected void Bindgv()    
  10. {    
  11.     SqlConnection con = new SqlConnection("data source=LENOVO;initial catalog=test;UID=sa;PWD=connect;");    
  12.   
  13.     con.Open();    
  14.     SqlCommand cmd = new SqlCommand("Select EmpId, EmpName, Company, Address from tbl_Emp", con);    
  15.     SqlDataAdapter da = new SqlDataAdapter(cmd);    
  16.     DataSet ds = new DataSet();    
  17.     da.Fill(ds);    
  18.     con.Close();    
  19.     gvEmp.DataSource = ds;    
  20.     gvEmp.DataBind();    
  21.   
  22. }    
  23. //for get record    
  24. protected void btnGetRecord_Click(object sender, EventArgs e)    
  25. {    
  26.     string str = string.Empty;    
  27.     string strname = string.Empty;    
  28.     foreach (GridViewRow gvrow in gvEmp.Rows)    
  29.     {    
  30.         CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");    
  31.         if (chk != null & chk.Checked)    
  32.         {    
  33.             str += "<b>EmpId :- </b>" + gvrow.Cells[1].Text + ", ";    
  34.             str += "<b>EmpName :- </b>" + gvrow.Cells[2].Text + ", ";    
  35.             str += "<b>Company :- </b>" + gvrow.Cells[3].Text + ", ";    
  36.             str += "<b>Addess :- </b>" + gvrow.Cells[4].Text;    
  37.             str += "<br />";    
  38.         }    
  39.     }           
  40.     lblRecord.Text = "<b>Selected EmpDetails: </b>" + str ;    
  41. }

Test the page as in the following:

data in grid view

grid view