Skip to main content
A newer version of this page is available. .
All docs
V21.2

TableView.AddNewRow(Boolean) Method

Adds a new row to the GridControl‘s data source.

Namespace: DevExpress.Xpf.Grid

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

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public void AddNewRow(
    bool showUpdateRowButtons = false
)

Optional Parameters

Name Type Default Description
showUpdateRowButtons Boolean False

If the Edit Entire Row mode and the New Item Row are enabled, you can set this parameter to true to allow users edit values in the new row before posting this row to the data source.

Remarks

Use the AddNewRow method to add a new row to the grid’s data source. The View reflects changes made in the data source and focuses the new record.

The GridControl does not move focus to the new record if a user focuses the Automatic Filter Row. To avoid this behavior, you can manually focus a data row and call the AddNewRow method (for example, you can use the DataViewBase.MoveLastRow method):

if (tableView.FocusedRowHandle == DataControlBase.AutoFilterRowHandle)
    tableView.MoveLastRow();
tableView.AddNewRow();

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

Example

The following example demonstrates how to add a new row to the GridControl and set this row’s values:

DevExpress WPF | Grid Control - Add and Remove Rows

View Example: How to Add and Remove Rows in Code

  1. Call the AddNewRow method to add a new row.

    This method moves focus to the New Item Row. If the New Item Row is disabled, the AddNewRow method adds an empty row at the bottom.

    <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" ItemsSource="{Binding PersonList}">
        <dxg:GridControl.View>
            <dxg:TableView x:Name="view"/>
        </dxg:GridControl.View>
    </dxg:GridControl>
    <!-- -->
    <Button Click="addNewRow">Add a New Row</Button> 
    
    void addNewRow(object sender, RoutedEventArgs e) {
        view.AddNewRow();
    }
    

    You can also use the TableViewCommands.AddNewRow command to add a new row and the GridViewBase.AddingNewRowCommand property to specify a data object and initialize its values.

  2. Use the DataControlBase.NewItemRowHandle to get the new row and set its values.

    void addNewRow(object sender, RoutedEventArgs e) {
        view.AddNewRow();
        int newRowHandle = DataControlBase.NewItemRowHandle;
        grid.SetCellValue(newRowHandle, "ProductName", "New Product");
        grid.SetCellValue(newRowHandle, "CompanyName", "New Company");
        grid.SetCellValue(newRowHandle, "UnitPrice", 10);            
        grid.SetCellValue(newRowHandle, "Discontinued", false);
    } 
    

    Refer to the following help topic for more information: Obtain and Set Cell Values in Code.

  3. After values are set and accepted, the new row moves according to the current filter, group, and sort settings.

See Also