Blogs

The Progress Bar - DevExpress XPF Blog

Silverlight Data Grid – Binding, Editing, Saving Data Using WCF RIA Services

     

I have covered data binding the Silverlight Grid Control using WCF RIA Services in the past, but have left out a common scenario where a user might need to update cells using in-place editors and save that data back to the data source.

So in this blog post, we’re going to take a look at how to use the combo box editor to change the data for a field and then save that change back to the database. Here I’m assuming the presence of a DXGrid for Silverlight control bound to the “Products” table of the sample Northwinds database. The “ProductName” column should have a combobox in-place editor as well. If you are just starting out, I recommend reading the following posts first before you proceed further:

  1. Silverlight Grid Control - Data Binding using RIA Services
  2. Silverlight Grid Control – How to Embed Editors

Updates do not occur automatically, so to save each change, I need a mechanism where the whole process is correctly automated. I’ll do this by handling the HiddenEditor and RowUpdated events of the TableView.

  1. <dxg:GridControl Name="gridControl1">
  2.     <dxg:GridControl.View>
  3.         <dxg:TableView AutoWidth="True" Name="tableView" HiddenEditor="tableView_HiddenEditor" RowUpdated="tableView_RowUpdated" />
  4.     </dxg:GridControl.View>
  5.     <dxg:GridControl.Columns>
  6.         <dxg:GridColumn FieldName="ProductName" Header="Product Name">
  7.             <dxg:GridColumn.EditSettings>
  8.                 <dxe:ComboBoxEditSettings DisplayMember="ProductName" ValueMember="ProductName" />
  9.             </dxg:GridColumn.EditSettings>
  10.         </dxg:GridColumn>
  11.         <dxg:GridColumn FieldName="UnitPrice" Header="Unit Price" />
  12.         <dxg:GridColumn FieldName="UnitsInStock" Header="Units in Stock" />
  13.         <dxg:GridColumn FieldName="UnitsOnOrder" Header="Units on Order" />
  14.     </dxg:GridControl.Columns>
  15. </dxg:GridControl>

The HiddenEditor event is invoked every time an editor is closed. So in this case, when an item in the combo box is selected and the editor is closed, the HiddenEditor event will be triggered.

The RowUpdated event is where we’ll submit the changes back to the database.

Here’s the code I’ll add to handle saving and submitting the data:

  1. private void tableView_HiddenEditor(object sender, DevExpress.Xpf.Grid.EditorEventArgs e)
  2. {
  3.     if (e.Column.FieldName != "ProductName") return;
  4.     gridControl1.View.CommitEditing();
  5. }
  6.  
  7. private void tableView_RowUpdated(object sender, DevExpress.Xpf.Grid.RowEventArgs e)
  8. {
  9.     _productsContext.SubmitChanges(OnSubmitCompleted, null);
  10. }
  11.  
  12. private void OnSubmitCompleted(SubmitOperation so)
  13. {
  14.     if ((so.HasError))
  15.     {
  16.         MessageBox.Show(string.Format("Save Failed: {0}", so.Error.Message));
  17.         so.MarkErrorAsHandled();
  18.     }
  19.     else
  20.     {
  21.         MessageBox.Show("Data Saved");
  22.     }
  23. }

The code in the OnSubmitCompleted method is purely for demonstration and error handling purposes.

And that’s it! You can now run the application and use the combo box to select and update the values in the “ProductName” column of the grid.

Stay tuned for a video demonstration of this tutorial on the DevExpress Channel.

Published Jan 28 2011, 03:47 PM by Emil Mesropian (DevExpress)
Bookmark and Share

Comments

 

Rick Bartlett said:

Can you show us this functionality using only mvvm?

Thanks

January 29, 2011 10:53 AM
 

The Progress Bar - DevExpress XPF Blog said:

Last week I posted a tutorial on how to use a combo box editor control to update and save the data for

January 31, 2011 2:08 PM
 

Geoff Hardy said:

Thanks for the sample. Some other things I would be interested is seeing here are:

1. How would you set focus back on the row that had the error?

2. If the errors apply to specific columns, is it possible to show the error message next to each column, like the standard Silverlight DataGrid does?

February 7, 2011 4:22 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.