I have a datagrid that gets filled serverside with he DataSource.
When deleting a item I use the RadGrid1_DeleteCommand event and apply the following code to get the item I have to delete.
void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem item = (GridEditableItem)e.Item;
dynamic DynamicBusinessObject = (item.OwnerTableView.DataSource as IEnumerable<dynamic>).ToList()[item.DataSetIndex];
This normally works great however when the user applies a filter to a column the DataSetIndex returns me the index-number of filtered page, not the corresponding index in the item.OwnerTableView.DataSource.
Much like ItemIndex will do when I’m on another page then page 1 within a grid.
How to get the corresponding item.OwnerTableView.DataSource index-number (or object) of the item.
5 Answers, 1 is accepted
You can get the relevant item by using its DataKeyValue in the following way:
GridEditableItem item = (GridEditableItem)e.Item;
int
id = (
int
)item.GetDataKeyValue(
"ID"
);
//assuming the ID field is set as DataKeyName for the Grid
dynamic DynamicBusinessObject = (item.OwnerTableView.DataSource
as
IEnumerable<dynamic>).ToList().SingleOrDefault(i => i.ID == id);
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Can you provide more details on the error? At which line exactly the exception is thrown? You should make sure that you are referencing the field by the exact same way including small and capital letters and that you have set it in the DataKeyNames property of the MasterTableView.
<
MasterTableView
DataKeyNames
=
"ID"
... >
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

<
MasterTableView
DataKeyNames
=
"ID"
... > in my apsx file.
The exception is thrown at the line `int
id = (
int
)item.GetDataKeyValue(
"Id"
);
Regardless of how you bind the grid - server side or through the markup you should have set the DataKeyNames property to the specific field that you wish to retrieve with the GetDataKeyValue method. You can set the DataKeyNames property either in the markup or in code behind (page_init, page_load events) even if you have server-side binding. Be sure to match the case of the field exactly so that it can be retrieved correctly.
Best wishes,Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.