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

How to update RadGrid bound to List of Objects using Advanced Binding (needdatasource)

2 Answers 412 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kris
Top achievements
Rank 1
Kris asked on 26 Mar 2010, 08:45 PM
New to Telerik, so forgive my ignorances but I am stuck on how to update data in my grid.

I have a grid that uses the NeedDataSource method to bind to a list of objects
<MasterTableView DataKeyNames="Id" Width="100%" AutoGenerateColumns="False"  
OnNeedDataSource="RadGrid1_NeedDataSource"


With my method like:
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e){  
IList<FXGroup> FXData = new List<FXGroup>();  
RadGrid1.DataSource = FXData;  



This is properly populating my grid.

I have set up the grid so that a doubleclick event on a line runs a client side script:
        function GridRowDblClick(sender, eventArgs) {  
            editedRow = eventArgs.get_itemIndexHierarchical();  
            $find("<%= RadGrid1.MasterTableView.ClientID %>").editItem(editedRow);  
        }  

This in turn opens my grid with an edit control, as defined in my mark up:
<telerik:GridBoundColumn DataField="Quantity" HeaderText="Qty"  
SortExpression="Quantity" UniqueName="Quantity" ReadOnly="False" ColumnEditorID="GridQuantityEditor" EditFormColumnIndex="0" >    
</telerik:GridBoundColumn>   
... 
<telerik:GridNumericColumnEditor ID="GridQuantityEditor" runat="server" NumericTextBox-Width="50px" NumericTextBox-NumberFormat-DecimalDigits="0" /> 

What I can not figure out is now what?  I tried to add an event to the grid
OnUpdateCommand="RadGrid1_UpdateCommand" 

 And in here I am not sure what all I need to do.  In order to do what I need I must call a .Save() event on my Object that is contained in the grid, in order to persist back to a database. I would think that in my code I could just do something like:
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        GridEditableItem editedItem = e.Item as GridEditableItem; 
        GridEditManager editMan = editedItem.EditManager; 
    object item = GetObjectByID(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Id"].ToString()); 
    item.Save(); 

This of course doesn't do anything.  My edited values do not get passed back into the entity.  Do I need to actually do something to pass the edit control values into my object?  How can I account for the various data types that the object's properties contain?

As you can probably tell I am pretty confused.  All of the samples that I have found discuss updates only against grids with defined datasourceids using what appears to be automatic updates.  I would hope that with a grid bound the way I have it that the grid edits would update the object it is bound to and I would just need to do the final  object .Save event.  Doesn't seem to be the case.



2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 31 Mar 2010, 09:53 AM
Hello Kris ,

You can use GridEditableItem's UpdateValues method passing your entity object. This will map the entity properties to the extracted values from the edit item. You can check this online demo, which demonstrates similar functionality using LinqToSql.

Regards,
Rosen
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.
0
Kris
Top achievements
Rank 1
answered on 31 Mar 2010, 01:05 PM
Awesome!  Thanks so much Rosen!  That worked. 

I have to say these RadControls completely rock!
Tags
Grid
Asked by
Kris
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Kris
Top achievements
Rank 1
Share this question
or