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

various ways to get to edited/inserted item row?

2 Answers 57 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Margo Noreen asked on 06 Apr 2010, 07:46 PM
I'm trying to use a RadGrid control with a manual datasource.  Tthat is, I do not have a DataSource control on the page, instead I use the NeedDataSource event to wire it up to a DataTable that is retrieved through some custom code.  The DataTable is is stashed in ViewState the first time and then retrieved on this event as needed.

Now, it's pretty straightforward to add a column to the RadGrid that adds an Edit link that puts the row into edit mode and that action column now has Update/Cancel links.  I can also see how easy it ease to add a AddNewRecords attribute to the CommandItemsSettings element.

And of course, wire up actions to the ItemCommand event or the individual UpdateCommand, DeleteCommand, etc. events.  So far so good.  What took quite a bit of digging into example code to find was the way to retrieve a reference to the row that was inserted/edited/deleted.  There seems to be a variety of ways, but the one I see most in example code is:

GridEditableItem eeditItem = e.Item as GridEditableItem;  
string str = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["EntityID"].ToString(); 

I have to tell you... I would never have figured this out w/out digging into existing code samples!  This does not seem intuitive at all, and so far, I haven't really come across any narratives or field notes or just plain english description about this.  Specifically,  I'm not sure I understand the OwnerTableView bit of it.

From the example above, why can't I just use the editItem.ItemIndex value to tell me which row in my datasource I'm on?  Then get that just index into it using something like 

DataTable dt = GridSource;  
GridEditableItem eeditItem = e.Item as GridEditableItem;  
DataRow myRow = dt.Rows[editItem.ItemIndex]; 

 I've also seen some sample code that does something like:

 
GridDataItem item = (GridDataItem)e.Item;    
string str = item["CompanyName"].Text;   

To me, both of the above examples are fairly easy to understand.

Any guidance (with some explanation) on what the various methods are would be appreciated.  Any info on the object chain that requires this OwnerTableView reference would also be greatly appreciated.  I realize I can just copy/paste the code and go on, but I'd really like to *understand* what I'm doing here!

Thanks.

2 Answers, 1 is accepted

Sort by
0
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
answered on 08 Apr 2010, 09:45 PM
I've also discovered yet another technique, that I assume is equivalent.  In the RadControls help file, there's a help topic under

Controls->RadGrid->ASP.NET 3.5 Features->LINQ To SQL - binding and manual CRUD operations.

So, it looks like I can just call the GetDataKeyValues and UpdateValues methods off of the appropriate GridItem.  This seems to simplify the code dramatically.  But I'm still not sure I understand it 100%.  Is the code listed in this help topic essentially the same thing as going through the OwnerTableView.DataKeyValues method and then the ExtractValues method followed by some sort of foreach loop?

Any guidance would be appreciated.
0
Iana Tsolova
Telerik team
answered on 09 Apr 2010, 03:43 PM
Hi Margo,

Basically there are two ways to get the data key value of a GridItem: one is through the DataKeyValues collection of the GridTableView to which the GridItem belongs to, and the other is directly through the GridItem using the GetDataKeyValue() method, as you already found. Both approaches are equal and it is your choice which to use.

Regarding the ExtractValues() method: It helps you retrieve the newly entered values from the GridEditableItem upon update.

I hope this helps.

Greetings,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Answers by
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Iana Tsolova
Telerik team
Share this question
or