Want to Add New Row when Tab key is pressed from the last column Instead Of Enter Key & move the CurrentCell to the first column of the AddNewRow

[1] Add New Row  when Tab key is pressed from the last column Instead Of Enter Key & move the CurrentCell to the first column of the AddNewRow
[2]How to move the CurrentCell to the first column of the AddNewRow when the Tab key is pressed from the last column and its position is at the Bottom of the SfDataGrid?
[3] Added Row should be available in sfDataGrid.View

3 Replies

AA Arulraj A Syncfusion Team August 15, 2018 12:28 PM UTC

Hi Avinash, 

Thanks for contacting Syncfusion support. 

Queries 
Response 











Add New Row when Tab key is pressed from the last column Instead Of Enter Key & move the CurrentCell to the first column of the AddNewRow 
We cannot handle the new row adding with handling keys. But the reported scenario of adding the new row while adding data in the last column can be done by handling the RowValidating event. Please refer to the following code. 

Code Example: 
// Wiring event. 
sfDataGrid1.RowValidating += SfDataGrid1_RowValidating; 
 
private void SfDataGrid1_RowValidating(object sender, RowValidatingEventArgs e) 
{ 
    if (this.sfDataGrid1.IsAddNewRowIndex(e.DataRow.RowIndex)) 
    { 
        var lastColumnIndex = sfDataGrid1.Columns.IndexOf(sfDataGrid1.Columns.LastOrDefault()); 
        var hasDetailsView = sfDataGrid1.DetailsViewDefinitions != null && sfDataGrid1.DetailsViewDefinitions.Count > 0; 
        var rowHeader = sfDataGrid1.ShowRowHeader; 
        var indentCount = sfDataGrid1.View.GroupDescriptions.Count; 
 
        var lastCellIndex = lastColumnIndex + indentCount + Convert.ToInt32(rowHeader) + Convert.ToInt32(hasDetailsView); 
 
        if (this.sfDataGrid1.CurrentCell.ColumnIndex != lastCellIndex) 
            e.IsValid = false; 
    } 
} 
 
 







How to move the CurrentCell to the first column of the AddNewRow when the Tab key is pressed from the last column and its position is at the Bottom of the SfDataGrid? 

Since we cannot handle the add new process in Key handling, we can move the current cell to first cell of newly added row after a row is added through RowValidated event. Please refer to the following code. 

Code Example: 
// Wiring event. 
sfDataGrid1.RowValidated += SfDataGrid1_RowValidated; 
 
private void SfDataGrid1_RowValidated(object sender, RowValidatedEventArgs e) 
{ 
    if (this.sfDataGrid1.IsAddNewRowIndex(e.DataRow.RowIndex)) 
    { 
        int firstColumnIndex = sfDataGrid1.Columns.IndexOf(sfDataGrid1.Columns.FirstOrDefault(x => x.Visible && x.ActualWidth != 0)); 
        var hasDetailsView = sfDataGrid1.DetailsViewDefinitions != null && sfDataGrid1.DetailsViewDefinitions.Count > 0; 
        var rowHeader = sfDataGrid1.ShowRowHeader; 
        var indentCount = sfDataGrid1.View.GroupDescriptions.Count; 
 
        var firstCellIndex = firstColumnIndex + indentCount + Convert.ToInt32(rowHeader) + Convert.ToInt32(hasDetailsView); 
        this.sfDataGrid1.MoveToCurrentCell(new RowColumnIndex(e.DataRow.RowIndex, firstCellIndex)); 
    } 
} 
Added Row should be available in sfDataGrid.View 
Yes, the added row will be available in sfDataGrid.View.Records once it is committed. 

Sample Link: 

Arulraj A 



AV Avinash August 16, 2018 10:50 AM UTC

Thanks it helped. But is there any control to achieve this functionality ?


AA Arulraj A Syncfusion Team August 17, 2018 12:31 PM UTC

Hi Avinash,  
 
Thanks for your update. 
 
We deeply regret to say that we do not have a control to achieve such a requirement, so we suggest you to use SfDataGrid with provided workaround in our previous update. 
 
Arulraj A 


Loader.
Up arrow icon