Skip to main content
A newer version of this page is available. .
All docs
V21.2

TableView.InitNewRowCommand Property

Gets or sets a command that allows you to initialize a new row with default values.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v21.2.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public ICommand<InitNewRowArgs> InitNewRowCommand { get; set; }

Property Value

Type Description
DevExpress.Mvvm.ICommand<InitNewRowArgs>

A command that allows you to initialize a new row with default values.

Remarks

Bind a command to the InitNewRowCommand property to maintain a clean MVVM pattern. The command works like an InitNewRow event handler and allows you to initialize a new row in a View Model.

The GridControl executes a command bound to the InitNewRowCommand property in the following cases:

This command is executed after the GridControl adds a new record to your data source. Use a command bound to the InitNewRowCommand property to initialize fields in the new record. For example, you can assign a unique value to the key field or assign default field values.

Refer to the following help topic for more information: Add and Remove Rows.

Example

The following example demonstrates how to initialize cells displayed within the New Item Row with default values:

DevExpress WPF | Grid Control - Initialize New Row MVVM

View Example: How to Initialize the New Item Row with Default Values

<dxg:GridControl x:Name="grid"
                 AutoGenerateColumns="AddNew"
                 ItemsSource="{Binding ProductList}">
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view"
                       AutoWidth="True" 
                       NewItemRowPosition="Top"
                       InitNewRowCommand="{Binding InitNewRowCommand}"
                       ValidateRowCommand="{Binding ValidateRowCommand}" 
                       InvalidRowExceptionCommand="{Binding InvalidRowCommand}" />
    </dxg:GridControl.View>
</dxg:GridControl>
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.Xpf;
// ...
public class MainViewModel : ViewModelBase {
// ...
    [Command]
    public void InitNewRow(InitNewRowArgs args) {
        var product = (Product)args.Item;
        product.UnitPrice = 10;
        product.CompanyName = "newcompany";
        product.Discontinued = false;
    }
    [Command]
    public void ValidateRow(RowValidationArgs args) {
        if(args.IsNewItem && string.IsNullOrEmpty(((Product)args.Item).ProductName)) {
            args.Result = new ValidationErrorInfo("Please enter the Product Name.");
        }
    }
    [Command]
    public void InvalidRow(InvalidRowExceptionArgs args) {
        if(args.IsNewItem) {
            args.ErrorText = "Please enter the Product Name.";
            args.WindowCaption = "Input Error";
        }
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the InitNewRowCommand property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also