Skip to main content
A newer version of this page is available. .

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.

The Items Source Wizard adds data posting functionality when you enable the CRUD (Create, Read, Update, Delete) options.

Items Source Wizard - Settings

The Items Source Wizard: generates the following code:

  1. The TableView.ShowUpdateRowButtons property is set to OnCellEditorOpen. 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 commands are generated at runtime from the methods with the attribute Command attached to them. A generated command name follows the [MethodName]Command pattern.

    The ValidateRow command saves newly added rows and changes to the existing ones back to the database:

    [Command]
    public void ValidateRow(RowValidationArgs args) {
        var item = (Order)args.Item;
        if (args.IsNewItem)
            _Context.Orders.Add(item);
        _Context.SaveChanges();
    }
    

    The ValidateRowDeletion command saves row deletion back to the database:

    [Command]
    public void ValidateRowDeletion(ValidateRowDeletionArgs args) {
        var item = (Order)args.Items.Single();
        _Context.Orders.Remove(item);
        _Context.SaveChanges();
    }
    

    The DataSourceRefresh command fetches changes from the database and updates the grid’s content:

    [Command]
    public void DataSourceRefresh(DataSourceRefreshArgs args) {
        _ItemsSource = null;
        _Context = null;
        RaisePropertyChanged(nameof(ItemsSource));
    }
    

    The TableView‘s properties include bindings to the generated commands:

    <dxg:GridControl ItemsSource="{Binding Orders}">
        <!-- ... -->
        <dxg:GridControl.View>
            <dxg:TableView NewItemRowPosition="Top"
                ShowUpdateRowButtons="OnCellEditorOpen"
                ValidateRowCommand="{Binding ValidateRowCommand}"
                ValidateRowDeletionCommand="{Binding ValidateRowDeletionCommand}"
                DataSourceRefreshCommand="{Binding DataSourceRefreshCommand}"/>
        </dxg:GridControl.View>
    </dxg:GridControl>
    
See Also