Skip to main content

Lesson 3 - Post Changes to a Database

  • 2 minutes to read

This tutorial demonstrates how to edit data within the GridControl and save changes to the database. The tutorial is based on Lesson 2.

WPF Data Grid - Get Started - Post Changes

  1. Set the TableView.ShowUpdateRowButtons property to OnCellEditorOpen or OnCellValueChange. This turns on an edit mode that allows users to edit an entire row and then submit or cancel all changes at once.
  2. The GridControl saves changes locally and does not post them to a database. To save changes to the database, create a ValidateAndSave command in the View Model. The command calls the data context’s SaveChanges method:

    using DevExpress.Mvvm.DataAnnotations;
    using DevExpress.Mvvm.Xpf;
    // ...
    public class ViewModel : ViewModelBase {
        NorthwindEntities northwindDBContext;
        // ...
        [Command]
        public void ValidateAndSave(RowValidationArgs args) {
            northwindDBContext.SaveChanges();
        }
    }
    

    The command is generated at runtime from the method with the Command attribute. The generated command name follows the [MethodName]Command pattern.

  3. Bind the ValidateAndSave command to the GridViewBase.ValidateRowCommand property. The GridControl executes this command after a user clicks the Update button:

    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True" 
                       BestFitModeOnSourceChange="VisibleRows"
                       ShowUpdateRowButtons="OnCellEditorOpen"
                       ValidateRowCommand="{Binding ValidateAndSaveCommand}"/>
    </dxg:GridControl.View>
    

You can use the approach demonstrated in this tutorial to implement other CRUD operations. Refer to the following help topic for more information: CRUD Operations in a Data-Bound Grid.

See Also