Back to Devexpress

GridViewBase.AddingNewRow Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridviewbase-fc1f24c0.md

latest5.0 KB
Original Source

GridViewBase.AddingNewRow Event

Occurs when a new row is added to the GridControl and allows you to initialize the new record.

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event AddingNewEventHandler AddingNewRow
vb
Public Event AddingNewRow As AddingNewEventHandler

Event Data

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

PropertyDescription
NewObjectGets or sets the object to be added to the binding list.

Remarks

The AddingNewRow event occurs before the GridControl adds a new record to your data source. In this event handler, you can specify a data object and initialize its values.

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

xaml
<dxg:GridControl>
    <dxg:GridControl.View>
        <dxg:TableView NewItemRowPosition="Top" 
                       AddingNewRow="view_AddingNewRow" />
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
void view_AddingNewRow(object sender, System.ComponentModel.AddingNewEventArgs e) {
    e.NewObject = new Product(selectedCompany) {
        ProductName = "", 
        CompanyName = "New Company", 
        UnitPrice = 10, 
        Discontinued = false 
    };
}
vb
Private Sub view_AddingNewRow(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs)
    e.NewObject = New Product(selectedCompany) With {
        .ProductName = "",
        .CompanyName = "New Company",
        .UnitPrice = 10,
        .Discontinued = False
    }
End Sub

Note

The AddingNewRow event does not fire when you add a new row directly to a bound data source.

Refer to the following topic for information on how to add a new row to the GridControl: New Item Row.

If you want to maintain a clean MVVM pattern and specify a new data record in a View Model, create a command and bind it to the AddingNewRowCommand property.

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddingNewRow 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-initialize-new-item-row-with-default-values/CS/NewItemRow_CodeBehind/MainWindow.xaml#L14

xml
NewItemRowPosition="Top"
AddingNewRow="OnAddingNewRow"
ValidateRow="OnValidateRow"

wpf-data-grid-add-and-remove-rows-in-code/CS/AddRemoveRows/AddRemoveRowBehavior.cs#L37

csharp
base.OnAttached();
    AssociatedObject.AddingNewRow += OnAddingNewRow;
}

wpf-data-grid-add-and-remove-rows-in-code/VB/AddRemoveRows/AddRemoveRowBehavior.vb#L55

vb
MyBase.OnAttached()
    AddHandler AssociatedObject.AddingNewRow, AddressOf OnAddingNewRow
End Sub

See Also

AddingNewEventArgs Class

AddingNewRowCommand

GridViewBase Class

GridViewBase Members

DevExpress.Xpf.Grid Namespace