ASP.NET

Get the selected row's data from a GridView in code-behind (C#)

ASP.NET · · Published 2026-09-17

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}";
}

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

Official documentation