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
close icon

Add new row automaticaly after tab or enter in last column and last row

I want to add a new row automatically after user try's to tab or hit the enter key when they are in the last column of the last row in a SfDataGrid.

Thanks!

6 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team June 20, 2019 06:08 AM UTC

Hi Matthew, 
  
Thank you for using Syncfusion controls. 
  
You can able to achieve your requirement by overriding the RowSelectionController class if the SelectionUnit as Row, otherwise you have to overriding the CellSelectionController class. 
  
Please refer the below code for Row selection, 
  
sfDataGrid.SelectionUnit = SelectionUnit.Row; 
sfDataGrid.SelectionController = new RowSelectionControllerExt(sfDataGrid); 
  
public class RowSelectionControllerExt : RowSelectionController 
{ 
     SfDataGrid sfDataGrid; 
  
     public RowSelectionControllerExt(SfDataGrid sfDataGrid) 
         : base(sfDataGrid) 
     { 
         this.sfDataGrid = sfDataGrid; 
     } 
  
     protected override void ProcessEnterKey() 
     { 
         AddingNewRow(); 
         base.ProcessEnterKey(); 
     } 
  
     protected override voidProcessArrowKeysForSingleMultipleSelection(KeyEventArgs args) 
     { 
         if (args.KeyCode == Keys.Tab) 
             AddingNewRow(); 
         base.ProcessArrowKeysForSingleMultipleSelection(args); 
     } 
  
     private void AddingNewRow() 
     { 
         if (DataGrid.RowCount - 1 == DataGrid.CurrentCell.RowIndex && DataGrid.ColumnCount - 1 == DataGrid.CurrentCell.ColumnIndex) 
         { 
             sfDataGrid.View.AddNew(); 
             sfDataGrid.View.CommitNew(); 
         } 
     } 
} 
  
Please refer the below code for Cell or Any selection 
  
sfDataGrid.SelectionUnit = SelectionUnit.Cell; 
sfDataGrid.SelectionController = new CellSelectionControllerExt(sfDataGrid); 
  
public class CellSelectionControllerExt : CellSelectionController 
{ 
    SfDataGrid sfDataGrid; 
  
    public CellSelectionControllerExt(SfDataGrid sfDataGrid) 
        : base(sfDataGrid) 
    { 
        this.sfDataGrid = sfDataGrid; 
    } 
  
    protected override void ProcessEnterKey() 
    { 
        AddingNewRow(); 
        base.ProcessEnterKey(); 
    } 
  
    protected override voidProcessArrowKeysForSingleMultipleSelection(KeyEventArgs args) 
    { 
        AddingNewRow(); 
        base.ProcessArrowKeysForSingleMultipleSelection(args); 
    } 
  
    private void AddingNewRow() 
    { 
        if (DataGrid.RowCount - 1 == DataGrid.CurrentCell.RowIndex && DataGrid.ColumnCount - 1 == DataGrid.CurrentCell.ColumnIndex) 
        { 
            sfDataGrid.View.AddNew(); 
            sfDataGrid.View.CommitNew(); 
        } 
    } 
} 
  
 
Let us know whether this helps also if you need any further assistance on this.  
 
Regards, 
Jagadeesan


MA Matthew April 16, 2020 06:56 PM UTC

I want to do two things is SfDataGrid, winform, C#
  1. Add a new row automaticaly after user hit's the tab or enter key when they are in the last column and last row of SFDataGrid.
  2. AND I want to enter key to function as a tab key for ALL cells & rows in SFDataGrid
Jagadeesan Pichaimuthu [Syncfusion] answered how to do part 1 above (add new row if you hit enter/tab key in last column and row of SFDataGrid and know I need to do how to both #1 AND #2 not just #1

I am attaching a zip of my source code file

See code in RowSelectionControllerExt.cs which does Request #1 but not #2
New selection controler is set in Form1_load()
sfDataGrid1.SelectionController = new RowSelectionControllerExt(sfDataGrid1); 


Thanks!



Attachment: v2_get_value_by_row_column_44264d85.7z


SS Susmitha Sundar Syncfusion Team April 17, 2020 12:43 PM UTC

Hi Matthew, 

Thank you for the update. 

You can achieve your by overriding the HandleKeyOperations method in RowSelectionController.cs. Please refer the below KB document. 



Please check the sample and let us know if you need further assistance on this. 

Regards, 
Susmitha S 



MA Matthew replied to Matthew Rothman April 17, 2020 02:41 PM UTC

I want to do two things is SfDataGrid, winform, C#
  1. Add a new row automaticaly after user hit's the tab or enter key when they are in the last column and last row of SFDataGrid.
  2. AND I want to enter key to function as a tab key for ALL cells & rows in SFDataGrid
Jagadeesan Pichaimuthu [Syncfusion] answered how to do part 1 above (add new row if you hit enter/tab key in last column and row of SFDataGrid and know I need to do how to both #1 AND #2 not just #1

I am attaching a zip of my source code file

See code in RowSelectionControllerExt.cs which does Request #1 but not #2
New selection controler is set in Form1_load()
sfDataGrid1.SelectionController = new RowSelectionControllerExt(sfDataGrid1); 


Thanks!



Attachment: v2_get_value_by_row_column_44264d85.7z

Thanks!

Almost done....In the sample code you sent me, The FIRST time you hit enter or tab in last row, It add's a new row to the SfDataGrid BUT if you just put values in that new row column 1, then column 2, then hit enter after data entry into column 2 on this new last row...Nothing happens...It does not create a 2nd new row and I need it to create a 2nd new row etc..

I want user to be able to do fast continous input into the SfDataGRid and we are almost done just need that one more little piece of logic/code.

I appreciate the help.


MA Matthew April 17, 2020 10:28 PM UTC

FYI,

I figured out the final part I needed. Thanks again for the help. Consider this closed please.



SS Susmitha Sundar Syncfusion Team April 20, 2020 08:36 AM UTC

Hi Matthew, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Susmitha S 


Loader.
Live Chat Icon For mobile
Up arrow icon