WPF Data Grid and TreeList - Edit Entire Row (v19.2)

WPF Team Blog
25 November 2019

The newest versions of our WPF Data Grid and TreeList introduce a new edit mode - Edit Entire Row.

In default mode, changes made via in-place editors are immediately posted to your data source. Unlike default mode, Edit Entire Row requires users to press the Update button to explicitly post changes to your data source:

To activate Edit Entire Row mode, use the TableView.ShowUpdateRowButtons / TreeListView.ShowUpdateRowButtons property.

When you change cell values within a row, our WPF Data Grid / TreeList “freezes” the UI. You cannot navigate away from the edited row until you record or cancel changes.

To post changes made, click the Update button. If you click the Cancel button, changes will be discarded:

The Data Grid in our example is bound to Entity Framework:

public MainWindow() {
    InitializeComponent();
    var context = new IssuesContext();
    grid.ItemsSource = context.Issues.ToArray();
}

public class IssuesContext : DbContext { 
    // ... 
}

When you make changes to grid values, changes are made only to in-memory replicas, not to the actual data in the database. To save changes and intercept possible database errors, handle the GridViewBase.ValidateRow event and explicitly call SaveChanges on the DataContext:

<dxg:TableView ShowUpdateRowButtons="OnCellEditorOpen" 
               ValidateRow="TableView_ValidateRow" />
void TableView_ValidateRow(object sender, GridRowValidationEventArgs e) {
    var issue = (Issue)e.Row;
    using(var context = new IssuesContext()) {
        var result = context.Issues.SingleOrDefault(b => b.Id == issue.Id);
        if(result != null) {
            result.Subject = issue.Subject;
            result.Priority = issue.Priority;
            result.Votes = issue.Votes;
            result.Priority = issue.Priority;
            context.SaveChanges();
        }
    }
}

The database in our example does not allow you to save a row with an empty subject:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Issue>()
        .Property(x => x.Subject)
        .IsRequired();
}

If you try to specify an empty subject, the Data Grid will allow you to correct values or click the Cancel button to return the previous values:

Useful Resources

Your Feedback Matters

Please let us know what you think about the new feature, or if you have any additional requests regarding data editing.

no comments
No Comments

Please login or register to post comments.