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.v24.1.dll
NuGet Package: DevExpress.Wpf.Grid.Core
#Declaration
public ICommand<InitNewRowArgs> InitNewRowCommand { get; set; }
#Property Value
Type | Description |
---|---|
ICommand<Init |
A command that allows you to initialize a new row with default values. |
#Remarks
Note
We recommend that you use the Adding
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:
- A user starts to edit the New Item Row.
- A user presses the Append button in the Data Navigator.
- You call the TableView.AddNewRow method / TableViewCommands.AddNewRow command.
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:
<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";
}
}
}