All docs
V20.1
20.2 (EAP/Beta)
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

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.

  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:

  1. The GridControl saves changes locally and does not post them to a database. To save changes to the database, create a ValidateAndSaveCommand in the ViewModel. The command calls the data context's SaveChanges method:

    public class ViewModel : ViewModelBase {
        NorthwindEntities northwindDBContext;
        public ViewModel() {
            ...
            ValidateAndSaveCommand = new DelegateCommand(ValidateAndSave);
        }
        ...
        public DelegateCommand ValidateAndSaveCommand { get; }
        void ValidateAndSave() {
            northwindDBContext.SaveChanges();
        }
    }
    
  2. Assign the EventToCommand object to the View's Interaction.Behaviors collection, and subscribe the ValidateAndSaveCommand to the GridViewBase.ValidateRow event. The GridControl raises this event after a user clicks the Update button:

    <dx:ThemedWindow
        ...
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
        <dxg:TableView ShowUpdateRowButtons="OnCellEditorOpen" ...>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:EventToCommand EventName="ValidateRow"
                                       Command="{Binding Path=ValidateAndSaveCommand}"/>
            </dxmvvm:Interaction.Behaviors>
        </dxg:TableView>
    
See Also