This is a migrated thread and some comments may be shown as answers.

Server-site deleting items fails because of 'incorrect' DataSetIndex when the user uses a filter.

5 Answers 137 Views
Grid
This is a migrated thread and some comments may be shown as answers.
T
Top achievements
Rank 1
T asked on 10 May 2011, 09:59 AM
 

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

Sort by
0
Marin
Telerik team
answered on 13 May 2011, 12:45 PM
Hi,

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);

All the best,
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.

0
T
Top achievements
Rank 1
answered on 13 May 2011, 02:03 PM
My ID field is named 'Id' but using this code throws me a System.ArgumentOutOfRangeException.
0
Marin
Telerik team
answered on 13 May 2011, 06:33 PM
Hi,

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" ... >

Greetings,
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.

0
T
Top achievements
Rank 1
answered on 14 May 2011, 07:16 PM
Like I told im my first post I fill it serversite with the DataSource. So I don´t have a
<MasterTableView DataKeyNames="ID" ... > in my apsx file.


The exception is thrown at the line `int id = (int)item.GetDataKeyValue("Id");
0
Marin
Telerik team
answered on 17 May 2011, 07:23 PM
Hi,

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.

Tags
Grid
Asked by
T
Top achievements
Rank 1
Answers by
Marin
Telerik team
T
Top achievements
Rank 1
Share this question
or