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 . . . }
Brilliant, thanks 🙂
thanks alot. 🙂
how about this:
var id = (int)((GridDataItem)e.Item).GetDataKeyValue(“Id”);
e.Item.DataItem is null in DeleteCommand (and ItemCommand), so that will not work.