Skip to main content
All docs
V21.1

TableView.AddNewRow() Method

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

Namespace: DevExpress.Xpf.Grid

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

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public void AddNewRow()

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 TableView.InitNewRow event to initialize new row 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.

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

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.

See Also