We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add a new row continuously without any selection to next row in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid |
Published Date: May 13, 2016 |
Last Revised Date: March 29, 2021

When pressing the Enter key or Tab key in AddNewRow the selection will be moved to next row at once the new row has been added in WPF DataGrid (SfDataGrid). You can skip this selection using CurrentCellActivated event like below code example.

XAML

<syncfusion:SfDataGrid x:Name="dataGrid"
                               AddNewRowPosition="Top"
                               AllowEditing="True"
                               AllowFiltering="True"
                               AutoGenerateColumns="False"
                               CurrentCellActivated="datagrid_CurrentCellActivated"
                               ItemsSource="{Binding EmployeeDetails}">
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn MappingName="EmployeeName" />
                <syncfusion:GridTextColumn MappingName="EmployeeAge" />
                <syncfusion:GridTextColumn MappingName="EmployeeArea" />
                <syncfusion:GridTextColumn MappingName="EmployeeGender" />
                <syncfusion:GridTextColumn MappingName="ExperienceInMonth" />
                <syncfusion:GridCheckBoxColumn MappingName="Review" />
            </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

 

C#

private void datagrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args)
{
    //Skip the selection when it is moved to next row on adding new row.        
    bool needToMove = (dataGrid.IsAddNewIndex(args.PreviousRowColumnIndex.RowIndex)
        && !dataGrid.IsAddNewIndex(args.CurrentRowColumnIndex.RowIndex)
        && (Keyboard.IsKeyDown(Key.Enter) || Keyboard.IsKeyDown(Key.Tab)));
    Dispatcher.BeginInvoke(new Action(() =>
    {
        var gridModel = this.dataGrid.GetGridModel();
        var columnIndex = this.dataGrid.GetFirstColumnIndex();
        if (needToMove && gridModel != null)
        {
            this.dataGrid.MoveCurrentCell(new RowColumnIndex(gridModel.AddNewRowController.GetAddNewRowIndex(), columnIndex));
        }
    }), DispatcherPriority.ApplicationIdle);
}   

 

In UWP, you have to use Dispatcher.RunAsync method for instead of Dispatcher.BeginInvoke method like below code example.

C#

private void datagrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args)
{    
    //Skip the selection when it is moved to next row on adding new row.        
    bool needToMove = (dataGrid.IsAddNewIndex(args.PreviousRowColumnIndex.RowIndex)
        && !dataGrid.IsAddNewIndex(args.CurrentRowColumnIndex.RowIndex)
        && (Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Enter).HasFlag(CoreVirtualKeyStates.Down) || Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Tab).HasFlag(CoreVirtualKeyStates.Down)));
 
    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () =>
        {
            var gridModel = this.dataGrid.GetGridModel();
            var columnIndex = this.dataGrid.GetFirstColumnIndex();
            if (needToMove && gridModel != null)
            {
                this.dataGrid.MoveCurrentCell(new RowColumnIndex(gridModel.AddNewRowController.GetAddNewRowIndex(), columnIndex));
            }
        });a
}

View sample in GitHub.

 

 

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile