This example demonstrates how to bind the GridControl to the BindingSource and populate it with data retrieved from the WCF Data Services.
Entities and data context classes generated by Visual Studio when you add a service reference to WCF Data Service lacks automatic handling modifications performed to entities such as changing property values, creating new objects or deleting objects from the collection. A programmer must handle all these changes manually to ensure that all changes will be sent to the service. One can create extensions over generated classes and custom collections that can automatically post all changes to the data context, but this example demonstrates a simpler approach that can be used in small applications where it does not require using complex solutions for such tasks.
This example demonstrates how to use GridView events and the embedded navigator to properly update the Data Service.
Each time the user makes modifications in cells and commits a modified row, the GridView raises the RowUpdated event. This is the optimal moment to notify the data context that an entity was modified:
[C#]voidOnRowUpdated(objectsender,DevExpress.XtraGrid.Views.Base.RowObjectEventArgse){MyEntities.UpdateObject(e.Row);}
[VB.NET]Sub OnRowUpdated(ByVal sender AsObject, ByVal e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) Handles MyGridView.RowUpdated MyEntities.UpdateObject(e.Row)EndSub
The EmbeddedNavigator.ButtonClick event of the GridControl's embedded navigator allows you to handle the CREATE and DELETE operations.
[C#]usingDevExpress.XtraEditors;voidOnEmbeddedNavigatorButtonClick(objectsender,NavigatorButtonClickEventArgse){switch(e.Button.ButtonType){caseNavigatorButtonType.Append:e.Handled=true;MyEntities.AddObject("MyTableName",MyBindingSource.AddNew());break;caseNavigatorButtonType.Remove:MyEntities.DeleteObject(MyGridView.GetFocusedRow());break;}}
[VB.NET]Imports DevExpress.XtraEditorsSub OnEmbeddedNavigatorButtonClick(ByVal sender AsObject, ByVal e As NavigatorButtonClickEventArgs)SelectCase e.Button.ButtonTypeCase NavigatorButtonType.Append e.Handled = True MyEntities.AddObject("MyTableName", MyBindingSource.AddNew())Case NavigatorButtonType.Remove MyEntities.DeleteObject(MyGridView.GetFocusedRow())EndSelectEndSub
See also:
Updating the Data Service (WCF Data Services)
How to implement CRUD operations using DXGrid and WCF Data Services