Home Asp.net Get GridView Selected Row Data In C# Vb From Code-behind

Get GridView Selected Row Data In C# Vb From Code-behind

4
0

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:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<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:

using System.Data;

After that add the following code in .aspx.cs page to bind gridview:

protected void Page_Load(object sender, EventArgs e)
{
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:

protected void btnGetData_Click(object sender, EventArgs e)
{
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:

Imports System.Data

After that add the following code to .aspx.vb page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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:

Protected Sub btnGetData_Click(ByVal sender As Object, ByVal e As EventArgs)
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

Example Result

Get Asp.net GridView Selected Row Data Code-behind using C# Vb.net

Download Example

[wpdm_file id=31]

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!