In this article I have explained with an example, how to get the cells values from
Selected Row of
GridView in
ASP.Net using C# and VB.Net.
The
GridView will be assigned an
OnSelectedIndexChanged event handler and when the
GridView Row is selected, the values of each Cell of the
Selected GridView Row will be fetched and displayed.
Note: More articles on GridView
HTML Markup
The
HTML Markup consists of following controls:
GridView – For displaying data.
Columns
The
GridView consists of
BoundFIeld,
TemplateField and
ButtonField column.
TemplateField - The TemplateField column consists of ItemTemplate.
ItemTemplate - It consists of a
Label.
ButtonField – It consists of a
Select Button to select the
GridView Row.
There is also a
Label control below the
GridView for displaying the
Selected Row Cell values.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<u>Selected Row Values:</u>
<br />
<br />
<asp:Label ID="lblValues" runat="server" Text=""></asp:Label>
Namespaces
You will need to import the following namespaces.
C#
VB.Net
Binding the ASP.Net GridView control
Inside the
Page_Load event handler, an object of
DataTable is created.
Then,
three columns are added to the
DataTable Columns collection using the
AddRange method.
An Array of objects of type DataColumn is specified which will hold the name and the optional parameter Data Type i.e. the Type of the column.
Once the schema is ready i.e. all the columns are defined, some rows have been added using the Rows.Add method.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4,"Robert Schidner" , "Russia")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Fetching Cell Values of GridView Selected Row in ASP.Net
Inside the SelectedIndexChanged event handler, the BoundField Cell value is extracted using the Cells property.
For extracting the value of the TemplateField Cell, first the Label control is referenced and then the value is extracted.
Finally, both the values are displayed in
Label control.
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
//Accessing BoundField Column
string name = gvCustomers.SelectedRow.Cells[0].Text;
//Accessing TemplateField Column controls
string country = (gvCustomers.SelectedRow.FindControl("lblCountry")as Label).Text;
lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
}
VB.Net
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
'Accessing BoundField Column
Dim name As String = gvCustomers.SelectedRow.Cells(0).Text
'Accessing TemplateField Column controls
Dim country As String = TryCast(gvCustomers.SelectedRow.FindControl("lblCountry"), Label).Text
lblValues.Text = "<b>Name:</b> " & name & " <b>Country:</b> " & country
End Sub
Screenshot
Demo
Downloads