Skip to content

Latest commit

 

History

History
116 lines (81 loc) · 5.73 KB

File metadata and controls

116 lines (81 loc) · 5.73 KB

WPF Data Grid - Extend CRUD Operations

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:

Solution 1 - Undo Operations

This solution uses the UndoCRUDOperationsBehavior that allows users to undo the latest operation (create, update, or delete).

  1. Assign this behavior to the Data Grid's view.

  2. 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);
    }
  3. Allow users to call the behavior's UndoCommand.

    <dxb:BarButtonItem Content="Undo (Ctrl+Z)" Command="{Binding UndoCommand, ElementName=undoBehavior}"/>

Files to Look At

Solution 2 - Async CRUD Operations

This solution shows how to implement async CRUD operations:

  1. Create tasks that allow the Data Grid to work with the database asynchronously.
  2. 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>

Files to Look At

Solution 3 - Detail Collection Editing

The solution uses the DialogEditFormBehavior with a custom EditTemplate that allows users to edit detail data for each row in the Data Grid.

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)