Back to Devexpress

TableView.InitNewRow Event

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

latest9.5 KB
Original Source

TableView.InitNewRow Event

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 event InitNewRowEventHandler InitNewRow
vb
Public Event InitNewRow As InitNewRowEventHandler

Event Data

The InitNewRow event's data class is InitNewRowEventArgs. The following properties provide information specific to this event:

PropertyDescription
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
RowHandleGets the handle of the added row.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

Note

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

The GridControl raises the InitNewRow event in the following cases:

This event occurs after the GridControl adds a new record to your data source. Handle the InitNewRow event to initialize fields in the new record. For example, you can assign a unique value to the key field or assign default field values. To do this, use the GridControl.SetCellValue method.

If you want to maintain a clean MVVM pattern and initialize a new row in a View Model, create a command and bind it to the InitNewRowCommand property.

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

Example

This example demonstrates how to initialize cells displayed within the New Item Row with default values.

xaml
<dxg:GridControl x:Name="grid"
                 AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view"
                       AutoWidth="True" 
                       NewItemRowPosition="Top" 
                       InitNewRow="OnInitNewRow"
                       ValidateRow="OnValidateRow" 
                       InvalidRowException="OnInvalidRowException" />
    </dxg:GridControl.View>
</dxg:GridControl>
cs
void OnInitNewRow(object sender, InitNewRowEventArgs e) {
    grid.SetCellValue(e.RowHandle, "UnitPrice", 10);
    grid.SetCellValue(e.RowHandle, "CompanyName", "newcompany");
    grid.SetCellValue(e.RowHandle, "Discontinued", false);
}

void OnValidateRow(object sender, GridRowValidationEventArgs e) {
    if(e.RowHandle == GridControl.NewItemRowHandle) {
        e.IsValid = !string.IsNullOrEmpty(((Product)e.Row).ProductName);
        e.Handled = true;
    }
}

void OnInvalidRowException(object sender, InvalidRowExceptionEventArgs e) {
    if(e.RowHandle == GridControl.NewItemRowHandle) {
        e.ErrorText = "Please enter the Product Name.";
        e.WindowCaption = "Input Error";
    }
}
vb
Private Sub OnInitNewRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
    Me.grid.SetCellValue(e.RowHandle, "UnitPrice", 10)
    Me.grid.SetCellValue(e.RowHandle, "CompanyName", "newcompany")
    Me.grid.SetCellValue(e.RowHandle, "Discontinued", False)
End Sub

Private Sub OnValidateRow(ByVal sender As Object, ByVal e As GridRowValidationEventArgs)
    If e.RowHandle = DataControlBase.NewItemRowHandle Then
        e.IsValid = Not String.IsNullOrEmpty(CType(e.Row, Product).ProductName)
        e.Handled = True
    End If
End Sub

Private Sub OnInvalidRowException(ByVal sender As Object, ByVal e As InvalidRowExceptionEventArgs)
    If e.RowHandle = DataControlBase.NewItemRowHandle Then
        e.ErrorText = "Please enter the Product Name."
        e.WindowCaption = "Input Error"
    End If
End Sub

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

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.

wpf-data-grid-extend-crud-operations/CS/Undo/UndoCRUDOperationsBehavior.cs#L45

csharp
AssociatedObject.DataSourceRefresh += OnRefresh;
    AssociatedObject.InitNewRow += OnNewRowStarted;
}

wpf-data-grid-extend-crud-operations/VB/Undo/UndoCRUDOperationsBehavior.vb#L71

vb
AddHandler AssociatedObject.DataSourceRefresh, AddressOf OnRefresh
    AddHandler AssociatedObject.InitNewRow, AddressOf OnNewRowStarted
End Sub

See Also

Add and Remove Rows

Obtain and Set Cell Values in Code

AddingNewRow

InitNewNode

TableView Class

TableView Members

DevExpress.Xpf.Grid Namespace