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. <asp:Label ID="lblRecord" runat="server" />
  28. </div>
  29. </form>
  30. <script type="text/javascript">
  31. // for check all checkbox
  32. function CheckAll(Checkbox) {
  33. var GridVwHeaderCheckbox = document.getElementById("<%=gvEmp.ClientID %>");
  34. for (i = 1; i < GridVwHeaderCheckbox.rows.length; i++) {
  35. GridVwHeaderCheckbox.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked = Checkbox.checked;
  36. }
  37. }
  38. </script>
  39. </body>
  40. </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. con.Open();
  13. SqlCommand cmd = new SqlCommand("Select EmpId, EmpName, Company, Address from tbl_Emp", con);
  14. SqlDataAdapter da = new SqlDataAdapter(cmd);
  15. DataSet ds = new DataSet();
  16. da.Fill(ds);
  17. con.Close();
  18. gvEmp.DataSource = ds;
  19. gvEmp.DataBind();
  20. }
  21. //for get record
  22. protected void btnGetRecord_Click(object sender, EventArgs e)
  23. {
  24. string str = string.Empty;
  25. string strname = string.Empty;
  26. foreach (GridViewRow gvrow in gvEmp.Rows)
  27. {
  28. CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
  29. if (chk != null & chk.Checked)
  30. {
  31. str += "<b>EmpId :- </b>" + gvrow.Cells[1].Text + ", ";
  32. str += "<b>EmpName :- </b>" + gvrow.Cells[2].Text + ", ";
  33. str += "<b>Company :- </b>" + gvrow.Cells[3].Text + ", ";
  34. str += "<b>Addess :- </b>" + gvrow.Cells[4].Text;
  35. str += "<br />";
  36. }
  37. }
  38. lblRecord.Text = "<b>Selected EmpDetails: </b>" + str ;
  39. }

Test the page as in the following:

data in grid view

grid view