Skip to main content
All docs
V25.1
  • 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.v25.1.dll

    NuGet Package: DevExpress.Wpf.Grid.Core

    Declaration

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

    Property Value

    Type Description
    ICommand<InitNewRowArgs>

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

    Remarks

    Note

    We recommend that you use the AddingNewRowCommand property instead to initialize a new row with default values.

    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

    <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";
            }
        }
    }
    
    See Also