RadGrid for ASP.NET

Retrieving primary key field values for selected items Send comments on this topic.
Selecting grid items > How-to > Retrieving primary key field values for selected items

Glossary Item Box

In numerous occasions you may need to fetch the primary key field values for grid items (on delete command, selecting grid items server side, etc.). For this purpose you can use the DataKeyNames/DataKeyValues arrays of the respective GridTableView. You should specify the primary key field through the DataKeyNames attribute and then obtain its value for a grid item of your choice through the DataKeyValues attribute.

The sample case demonstrates below extracts the primary key value for selected grid row (through a select column) in the ItemCommand and SelectedIndexChanged handlers:


ASPX/ASCX Copy Code
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" AutoGenerateColumns="false"
runat="server">
<MasterTableView DataKeyNames= "CustomerID" DataSourceID="AccessDataSource1">
<Columns>
<rad:GridButtonColumn CommandName="Select" Text="Select" UniqueName="SelectColumn" />
<rad:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" />
<rad:GridBoundColumn UniqueName="ContactTitle" HeaderText="Contact title" DataField="ContactTitle" />
<rad:GridBoundColumn UniqueName="Address" HeaderText="Address" DataField="Address" />

</Columns>
</MasterTableView>
<SelectedItemStyle BackColor="Aqua" />
</
rad:RadGrid>
<
asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb"
SelectCommand="SELECT TOP 10 CustomerID, ContactName, ContactTitle, Address FROM Customers"
runat="server"></asp:AccessDataSource>

And in the code-behind:

VB.NET Copy Code
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
 If (e.CommandName = RadGrid.EditCommandName) Then
  Response.Write("Primary key for the clicked item from ItemCommand: " _
      + e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("CustomerID") + "<br>")
 End If
End Sub
Protected Sub RadGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.SelectedIndexChanged
 Response.Write("Primary key for the clicked item from SelectedIndexChanged: " _
     + RadGrid1.SelectedItems(0).OwnerTableView.DataKeyValues(RadGrid1.SelectedItems(0).ItemIndex)("CustomerID") + "<br>")
End Sub
C# Copy Code
protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.EditCommandName)
{
 Response.Write(
"Primary key for the clicked item from ItemCommand: " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerID"] + "<br>");
}
}
protected void RadGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Response.Write(
"Primary key for the clicked item from SelectedIndexChanged: "
    
+ RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex][ "CustomerID"] + "<br>");
}