Back to Devexpress

TableView.InitNewRowCommand Property

wpf-devexpress-dot-xpf-dot-grid-dot-tableview-e13a09fb.md

latest5.6 KB
Original Source

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.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public ICommand<InitNewRowArgs> InitNewRowCommand { get; set; }
vb
Public Property InitNewRowCommand As ICommand(Of InitNewRowArgs)

Property Value

TypeDescription
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:

xaml
<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>
cs
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";
        }
    }
}
vb
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm.Xpf
' ...
Public Class MainViewModel
    Inherits ViewModelBase
    ' ...
    <Command>
    Public Sub InitNewRow(ByVal args As InitNewRowArgs)
        Dim product = CType(args.Item, Product)
        product.UnitPrice = 10
        product.CompanyName = "newcompany"
        product.Discontinued = False
    End Sub

    <Command>
    Public Sub ValidateRow(ByVal args As RowValidationArgs)
        If args.IsNewItem AndAlso String.IsNullOrEmpty(CType(args.Item, Product).ProductName) Then
            args.Result = New ValidationErrorInfo("Please enter the Product Name.")
        End If
    End Sub

    <Command>
    Public Sub InvalidRow(ByVal args As InvalidRowExceptionArgs)
        If args.IsNewItem Then
            args.ErrorText = "Please enter the Product Name."
            args.WindowCaption = "Input Error"
        End If
    End Sub
End Class

See Also

AddingNewRowCommand

InitNewNodeCommand

TableView Class

TableView Members

DevExpress.Xpf.Grid Namespace