First of all you should set the MasterTableView.DataKeyNames property of the RadGrid to the primary key columns:
<MasterTableView DataKeyNames="Id">

<telerik:RadGrid ID="customerOverviewRadGrid" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="customersDS" EnableLinqExpressions="false" runat="server" ondeletecommand="customerOverviewRadGrid_DeleteCommand" > <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> <MasterTableView DataKeyNames="Id"> <Columns> . . . </Columns> </MasterTableView> </telerik:RadGrid>

Then in you’re DeleteCommand you can get you’re primary key value:

protected void customerOverviewRadGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
    int id = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
    // run code to delete customer in the database . . . 
}

Tags: ,

3 Comments on How to get the primary key value in DeleteCommand event of a RadGrid

  1. James says:

    Brilliant, thanks :)

  2. sepide says:

    thanks alot. :-)

  3. Rui says:

    how about this:
    var id = (int)((GridDataItem)e.Item).GetDataKeyValue(“Id”);

Leave a Reply