Managing datagrid with keyboard

Hi there!
I'm developing a WPF view with a grid and I would like to use the keyboard instead of mouse to control it. These are the features I have to accomplish:

- Using a combination of keys, enable or disable datagrid editing. Done
- When enablind datagrid, set the position in a new row with default values. Pending.
- Using keys to modify data, using tab/enter/arrow keys to move through cells. Done.
- When new row is completed, move to a new row with default values. Pending


Hope you can help me to accomplish pending features.


Thanks in advance

Victor

PD: I'm using MVVM approach



1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 14, 2021 02:48 PM UTC

Hi Aliquo,

Thank you for contacting Syncfusion Support.

Please find answer for your queries below 
Queries 
Solutions 
 
When enablind datagrid, set the position in a new row with default values 
 
 

Your requirement can be achieved by customize the Loaded and AddNewRowInitiating event in SfDataGrid. Please refer the below codes snippet, 

XMAL Code Snippet:

 
<syncfusion:SfDataGrid x:Name="sfDataGrid"  
                               AllowEditing="True"    
                               AddNewRowPosition="Top"  
                               CurrentCellActivated="sfDataGrid_CurrentCellActivated" 
                               AddNewRowInitiating="sfDataGrid_AddNewRowInitiating" 
                               Loaded="sfDataGrid_Loaded" 
                               ItemsSource="{Binding Orders}" 
                               AutoGenerateColumns="True">           </syncfusion:SfDataGrid>    
 

C# Code Snippet:

 
private void sfDataGrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs e) 
{            
            //set the default values for AddNewRow 
            var data = e.NewObject as OrderInfo; 
            data.OrderID = 0; 
            data.CustomerID = "Default"; 
            data.CustomerName = "Default"; 
            data.Country = "Default"; 
            data.UnitPrice = 0; 
} 
 
 
For more information related to AddNewRowInitiating event, please refer the user guide documentation,

 
private void sfDataGrid_Loaded(object sender, RoutedEventArgs e) 
{ 
            //When enablind datagrid, set the position in a new row with default values 
            var gridModel = this.sfDataGrid.GetGridModel(); 
            var columnIndex = this.sfDataGrid.GetFirstColumnIndex(); 
            if (gridModel != null) 
            { 
                this.sfDataGrid.MoveCurrentCell(new RowColumnIndex(gridModel.AddNewRowController.GetAddNewRowIndex(), columnIndex)); 
                //programmatically edit the NewRow 
                this.sfDataGrid.SelectionController.CurrentCellManager.BeginEdit(); 
            } 
} 

For more information related to Loaded event, please refer the user guide documentation,

 
 
 
When new row is completed, move to a new row with default values 
 
 
 
Your requirement can be achieved by customize the CurrentCellActivated event in SfDataGrid. Please refer the below codes snippet,

XMAL Code Snippet:

 
<syncfusion:SfDataGrid x:Name="sfDataGrid"  
                               AllowEditing="True"    
                               AddNewRowPosition="Top"  
                               CurrentCellActivated="sfDataGrid_CurrentCellActivated" 
                               AddNewRowInitiating="sfDataGrid_AddNewRowInitiating" 
                               Loaded="sfDataGrid_Loaded" 
                               ItemsSource="{Binding Orders}" 
                               AutoGenerateColumns="True">           </syncfusion:SfDataGrid>    
 
C# Code Snippet: 
 
private void sfDataGrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs e) 
{ 
            //When new row is completed, move to a new row with default values         
            bool needToMove = (sfDataGrid.IsAddNewIndex(e.PreviousRowColumnIndex.RowIndex) 
                && !sfDataGrid.IsAddNewIndex(e.CurrentRowColumnIndex.RowIndex) 
                && (Keyboard.IsKeyDown(Key.Enter) || Keyboard.IsKeyDown(Key.Tab))); 
            Dispatcher.BeginInvoke(new Action(() => 
            { 
                var gridModel = this.sfDataGrid.GetGridModel(); 
                var columnIndex = this.sfDataGrid.GetFirstColumnIndex(); 
                if (needToMove && gridModel != null) 
                { 
                    this.sfDataGrid.MoveCurrentCell(new RowColumnIndex(gridModel.AddNewRowController.GetAddNewRowIndex(), columnIndex)); 
                    //programmatically edit the NewRow 
                    this.sfDataGrid.SelectionController.CurrentCellManager.BeginEdit(); 
                } 
            }), DispatcherPriority.ApplicationIdle); 
} 
 
 
For more information related to CurrentCellActivated event, please refer the user guide documentation,

 
 

Sample Link: https://www.syncfusion.com/downloads/support/forum/166311/ze/Sample1328985798

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon