Get the selected row's data from a GridView in code-behind (C#)
When a user clicks Select on a GridView row, the server needs that row's values: an id to load a record, a name to show in a form. There are several ways to read them, and the most robust one does not read the cells at all.
The grid
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
DataKeyNames="CustomerId,Country" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged"
SelectedRowStyle-CssClass="is-selected">
<Columns>
<asp:CommandField ShowSelectButton="true" SelectText="Select" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:TemplateField HeaderText="Credit limit">
<ItemTemplate>
<asp:Label ID="lblLimit" runat="server" Text='<%# Eval("CreditLimit", "{0:N0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblInfo" runat="server" />
Reading the selection
protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvCustomers.SelectedRow;
// 1. Keys: reliable, typed, not shown in the grid
int id = (int)gvCustomers.SelectedDataKey.Values["CustomerId"];
string country = (string)gvCustomers.SelectedDataKey.Values["Country"];
// 2. A BoundField cell: text is HTML-encoded
string name = HttpUtility.HtmlDecode(row.Cells[1].Text);
// 3. A control in a TemplateField
string limit = ((Label)row.FindControl("lblLimit")).Text;
lblInfo.Text = $"#{id} {HttpUtility.HtmlEncode(name)} ({HttpUtility.HtmlEncode(country)}), limit {limit}";
}
- DataKeys are stored in control state and do not depend on column order or formatting. Use them for anything you will query with.
- Cells are indexed by column position, including the select column, and contain encoded, formatted text. An empty value appears as
. Fine for display, fragile for logic. - FindControl reads controls inside template fields by their id.
Buttons in the row instead of Select
A button with a custom command passes the row through CommandArgument:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Details" CommandName="ShowDetails"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "ShowDetails") return;
int index = Convert.ToInt32(e.CommandArgument) - gvCustomers.PageIndex * gvCustomers.PageSize;
int id = (int)gvCustomers.DataKeys[index].Value;
ShowCustomer(id);
}
Container.DataItemIndex counts across all pages, while DataKeys and Rows only hold the current page, hence the subtraction when paging is on. Alternatively use Container.DisplayIndex, which is the index within the page.
Styling the selected row
.is-selected td { background: #fff3c4; font-weight: 600; }
Reset the selection with gvCustomers.SelectedIndex = -1; after rebinding to other data, or the same row index stays selected for a different record.
A full form built on this pattern: Insert, edit, update and delete with a form and a GridView. Reading row values in the browser: GridView row values with jQuery.
Common mistakes
- Reading
SelectedRowinPage_Load: the selection event has not run yet. Read it inSelectedIndexChangedor later. - Rebinding the grid before reading the selection: rebinding recreates the rows and the selected index may then point at different data.
- Using
Cells[n].Textfor template columns: it is empty, because the content is in child controls. UseFindControl.
Official documentation
- Microsoft Learn: GridView.SelectedRow, GridView.SelectedDataKey, GridView.RowCommand