After you bind the Data Grid to a database, you can implement CRUD operations (create, read, update, delete). These operations allow you to post changes that users make in the Data Grid to the database.
This repository contains solutions that extend CRUD operations:
This solution uses the UndoCRUDOperationsBehavior that allows users to undo the latest operation (create, update, or delete).
-
Assign this behavior to the Data Grid's view.
-
Create a class that implements ICopyOperationsSupporter. The class instance allows the behavior to copy data item properties and apply them when users execute the undo operation.
<dxg:GridControl x:Name="grid" ItemsSource="{Binding ItemsSource}"> <dxg:GridControl.View> <dxg:TableView> <!-- ... --> <dxmvvm:Interaction.Behaviors> <local:UndoCRUDOperationsBehavior x:Name="undoBehavior" CopyOperationsSupporter="{Binding CopyOperationsSupporter}" /> </dxmvvm:Interaction.Behaviors> </dxg:TableView> </dxg:GridControl.View> </dxg:GridControl>
public interface ICopyOperationSupporter { object Clone(object item); void CopyTo(object source, object target); }
-
Allow users to call the behavior's UndoCommand.
<dxb:BarButtonItem Content="Undo (Ctrl+Z)" Command="{Binding UndoCommand, ElementName=undoBehavior}"/>
This solution shows how to implement async CRUD operations:
- Create tasks that allow the Data Grid to work with the database asynchronously.
- Assign these tasks to the DataSourceRefreshArgs.RefreshAsync, ValidationArgs.ResultAsync, DeleteValidationArgs.ResultAsync properties.
args.ResultAsync = Task.Run(async () => { await DoSomethingAsync(); });
Note that you also need to load initial data asynchronously. Use the EventToCommand behavior to execute the RefreshDataSource command in response to the Loaded event:
<dxg:GridControl ItemsSource="{Binding ItemsSource}">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand Event="Loaded"
Command="{Binding RelativeSource={RelativeSource Self},
Path=AssociatedObject.View.Commands.RefreshDataSource}"/>
</dxmvvm:Interaction.Behaviors>
<!-- ... -->
</dxg:GridControl>
The solution uses the DialogEditFormBehavior with a custom EditTemplate that allows users to edit detail data for each row in the Data Grid.
(you will be redirected to DevExpress.com to submit your response)