In my previous tutorials, I’d explained get gridview selected row hiddenfield value using jquery, how to keep gridview selected rows during paging, how to print gridview data on print button click event and other more cracking tutorials on GridView, Asp.net here.
Now here in this tutorial, I’ll explain how you can get gridview selected row data from code-behind or server-side in asp.net using c# and vb.net with example code and step by step guide.
HTML Markup To Get GridView Selected Row Data – [.aspx]
Following is the complete HTML markup for .aspx page:
<head id=”Head1″ runat=”server”>
<title>Get gridview selected row data on server-side in asp.net
using c# or vb.net</title>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function () {
//Checkbox header select all settings
$(‘[id$=chkSelectAll]’).click(function () {
if ($(this).is(“:checked”)) {
$(‘.chkSelect > input[type=”checkbox”]’).prop(‘checked’, true);
} else {
$(‘.chkSelect > input[type=”checkbox”]’).prop(‘checked’, false);
//remove selected class from each row
$(‘.chkSelect > input[type=”checkbox”]’).closest(“tr”).removeClass(“selected”);
}
});
});
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<h4>
Get gridview selected row data on server-side in asp.net using c# or vb.net</h4>
<div>
<asp:GridView ID=”grdResult” runat=”server” DataKeyNames=”SubjectId”>
<HeaderStyle Font-Bold=”true” BackColor=”#9a9a9a” BorderColor=”#222″ ForeColor=”White”
Height=”30″ />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID=”chkSelectAll” runat=”server” />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”chkSelect” runat=”server” CssClass=”chkSelect” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID=”btnGetData” runat=”server” OnClick=”btnGetData_Click”
Text=”Get Data” />
<br />
<br />
<b>You’d selected following rows:</b><br />
<br />
<asp:Label ID=”lblSelectedData” runat=”server” />
</div>
</form>
</body>
</html>
Make sure I’ve used runat=”server” property of checkbox to access and identify selected rows and also declared OnClick=”btnGetData_Click” event of asp.net button control to get row values from code-behind file.
After adding HTML markup to .aspx page, choose your language (that is C# or Vb.net) and paste it to your code-behind file.
Get GridView Selected Row Data In C# – [.cs]
Now add the following namespace:
After that add the following code in .aspx.cs page to bind gridview:
{
if (!IsPostBack)
{
LoadResultData();
}
}
private void LoadResultData()
{
DataTable dt = new DataTable();
//Creating Column
dt.Columns.Add(“SubjectId”);
dt.Columns.Add(“SubjectName”);
dt.Columns.Add(“Marks”);
//Adding Results in rows
dt.Rows.Add(1, “Asp.net”, 95);
dt.Rows.Add(2, “C#”, 88);
dt.Rows.Add(3, “Vb.net”, 78);
dt.Rows.Add(4, “HTML”, 89);
dt.Rows.Add(5, “CSS”, 90);
dt.Rows.Add(6, “JavaScript”, 85);
dt.Rows.Add(7, “jQuery”, 96);
//Binding Results to gridview
grdResult.DataSource = dt;
grdResult.DataBind();
}
And here is the code to get selected or checked row values from code-behind, add following code right after the above code:
{
lblSelectedData.Text = “”;
foreach (GridViewRow row in grdResult.Rows)
{
CheckBox chk = (CheckBox)row.FindControl(“chkSelect”);
if (chk != null & chk.Checked)
{
lblSelectedData.Text += ” <b>Subject:</b> “ + row.Cells[2].Text;
lblSelectedData.Text += ” <b>Marks:</b> “ + row.Cells[3].Text + “<br />”;
}
}
}
Get GridView Selected Row Data In Vb.net – [.vb]
Now add the following namespace:
After that add the following code to .aspx.vb page:
If Not IsPostBack Then
LoadResultData()
End If
End Sub
Private Sub LoadResultData()
Dim dt As New DataTable()
‘Creating Column
dt.Columns.Add(“SubjectId”)
dt.Columns.Add(“SubjectName”)
dt.Columns.Add(“Marks”)
‘Adding Results in rows
dt.Rows.Add(1, “Asp.net”, 95)
dt.Rows.Add(2, “C#”, 88)
dt.Rows.Add(3, “Vb.net”, 78)
dt.Rows.Add(4, “HTML”, 89)
dt.Rows.Add(5, “CSS”, 90)
dt.Rows.Add(6, “JavaScript”, 85)
dt.Rows.Add(7, “jQuery”, 96)
‘Binding Results to gridview
grdResult.DataSource = dt
grdResult.DataBind()
End Sub
And here is the code to get selected or checked row values from code-behind, add following code right after the above code:
lblSelectedData.Text = “”
For Each row As GridViewRow In grdResult.Rows
Dim chk As CheckBox = CType(row.FindControl(“chkSelect”), CheckBox)
If chk IsNot Nothing And chk.Checked Then
lblSelectedData.Text &= ” <b>Subject:</b> “ & row.Cells(2).Text
lblSelectedData.Text &= ” <b>Marks:</b> “ & row.Cells(3).Text & “<br />”
End If
Next row
End Sub