New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Retrieving Primary Key Field Values for Selected Items

There are many times you may need to fetch the primary key field values for grid items. You can use the DataKeyNames and DataKeyValues arrays of the GridTableView object for this purpose. When you specify a primary key field by adding it to the DataKeyNames array, the value of the specified column becomes available in DataKeyValues array.

The following example illustrates this use of DataKeyNames and DataKeyValues. The ItemCommand and SelectedIndexChanged event handlers use the DataKeyValues array to access the primary key of a row when it enters edit mode or when it is selected, respectively:

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"
  OnItemCommand="RadGrid1_ItemCommand" OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
  <MasterTableView DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
    <Columns>
      <telerik:GridButtonColumn CommandName="Select" Text="Select" UniqueName="SelectColumn" />
      <telerik:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" />
      <telerik:GridBoundColumn UniqueName="ContactTitle" HeaderText="Contact title" DataField="ContactTitle" />
      <telerik:GridBoundColumn UniqueName="Address" HeaderText="Address" DataField="Address" />
    </Columns>
  </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT TOP 10 [CustomerID], [ContactName], [ContactTitle], [Address] FROM [Customers]">
</asp:SqlDataSource>

See Also

In this article
See Also
Not finding the help you need?
Contact Support