• Javascript
  • Python
  • Go

Setting Selected Row in ASP.NET GridView Using DataKey

Names One of the most useful controls in ASP.NET for displaying tabular data is the GridView. It allows developers to easily bind data from ...

Names

One of the most useful controls in ASP.NET for displaying tabular data is the GridView. It allows developers to easily bind data from a data source and display it in a table format. However, when working with large datasets, it can be challenging to keep track of which row is selected. This is where the DataKeyNames property of the GridView comes in handy. In this article, we will explore how to use DataKeyNames to set the selected row in an ASP.NET GridView.

First, let's understand what the DataKeyNames property is. It is a property of the GridView control that allows you to specify the primary key field(s) of the data source. This means that when you bind data to the GridView, the values of these key fields will be stored in the DataKeys collection of the GridView. The DataKeys collection is essentially a dictionary that allows you to access the values of the key fields for each row in the GridView.

Now, let's see how we can use the DataKeyNames property to set the selected row in the GridView. To start, we need to have a GridView control on our page and bind it to a data source. For this example, we will use a simple SQLDataSource with a SELECT query to retrieve data from a database table. Our GridView will have three columns - ID, Name, and Age. The ID column will be our primary key field, and we will set it as the DataKeyNames property of the GridView.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID">

<Columns>

<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />

<asp:BoundField DataField="Name" HeaderText="Name" />

<asp:BoundField DataField="Age" HeaderText="Age" />

</Columns>

</asp:GridView>

Next, we need to handle the RowDataBound event of the GridView. This event is fired for each row in the GridView when it is being bound to data. In the event handler, we can access the DataKeys collection and retrieve the value of the primary key field for the current row. We can then compare this value with the selected value from our dropdown list or any other input control and set the Selected property of the row accordingly.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

string id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

int selectedId = int.Parse(ddlIds.SelectedValue);

if (id == selectedId.ToString())

{

e.Row.Selected = true;

}

}

}

In the above code, we are first checking if the current row is of type DataRow. This is to ensure that we only set the Selected property for data rows and not the header or footer rows. Then, we retrieve the value of the primary key field for the current row and compare it with the selected value from our dropdown list. If they match, we set the Selected property of the row to true.

That's it! Now when the GridView is rendered, the row with the matching primary key value will be selected. This can be useful when working with GridView paging, where the selected row needs to be preserved when navigating between pages.

In conclusion, the DataKeyNames property

Related Articles