Add New Row Functionality

[1] Want to Add Only Blank Row(With AddNewRow Functionality) at Page Load To SfDataGrid With Add New Row Placed at position is at the FixedBottom .
[2] Move Cursor To FirstCell In Edit Mode.
[2] Want to Commit Row at AddNewRowInitiating Event, So that it should be available in sfDataGrid.View
[3] Want to other columns value based on first or second column
[4] 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 in Edit Mode.

13 Replies

AA Arulraj A Syncfusion Team August 17, 2018 01:18 PM UTC

Hi Avinash, 

Thanks for using Syncfusion product. 

Query 
Response 
Want to Add Only Blank Row(With AddNewRow Functionality) at Page Load To SfDataGrid With Add New Row Placed at position is at the FixedBottom . 
If you want to add the blank records to the new row, you can use the RowValidating event. In which you need to check whether all the fields have the blank values as given in the below code sample, 

Code Sample: 
void sfDataGrid_RowValidating(object sender, Syncfusion.WinForms.DataGrid.Events.RowValidatingEventArgs e) 
{ 
    if(sfDataGrid.AddNewRowPosition == RowPosition.Bottom) 
    { 
        var data = e.DataRow.RowData as OrderInfo; 
 
        //Loop and check all the columns having the empty values for the added record. 
        if (!string.IsNullOrEmpty(data.CustomerID) || !string.IsNullOrEmpty(data.ProductName) || !string.IsNullOrEmpty(data.ShipCountry)) 
        { 
            e.ErrorMessage = "Records needs to be added empty for the AddNewRowPosition as Bottom"; 
            e.IsValid = false; 
        } 
    }        
} 
 
Move Cursor To FirstCell In Edit Mode. 
If you want to move the cursor to the first cell on the edit mode, you can use the begin edit event. And change the cursor location to the position as given in the below code sample. 

Code Sample: 
sfDataGrid.CurrentCellBeginEdit += sfDataGrid_CurrentCellBeginEdit; 
void sfDataGrid_CurrentCellBeginEdit(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellBeginEditEventArgs e) 
{ 
    var rectangle = sfDataGrid.TableControl.GetCellRectangle(e.DataRow.RowIndex, e.DataRow.VisibleColumns.FirstOrDefault().Index, false); 
    Cursor.Position = sfDataGrid.TableControl.PointToScreen(rectangle.Location); 
 
} 
 
Want to Commit Row at AddNewRowInitiating Event, So that it should be available in sfDataGrid.View 
If you want to add the object to the view using the AddNewRowInitiating event, you can use the View.Records.Add method. It will add the given data object to the records collection. 

Code Sample: 
sfDataGrid.AddNewRowInitiating += sfDataGrid_AddNewRowInitiating; 
void sfDataGrid_AddNewRowInitiating(object sender, Syncfusion.WinForms.DataGrid.Events.AddNewRowInitiatingEventArgs e) 
{ 
    if (e.NewObject != null) 
    { 
        //To set the default value for the added new item. 
        (e.NewObject as OrderInfo).OrderID = 7; 
 
        //To add the object to the view. 
        sfDataGrid.View.Records.Add(e.NewObject); 
    } 
} 
 
Want to other columns value based on first or second column 
If you want to modify the other column values based on any other column, you can use the EndEdit event and will change the row data of the AddNewRow based on the given value. 

Code Example: 
sfDataGrid.CurrentCellEndEdit += sfDataGrid_CurrentCellEndEdit; 
void sfDataGrid_CurrentCellEndEdit(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellEndEditEventArgs e) 
{ 
    if (e.DataRow.RowType == RowType.AddNewRow && e.DataColumn.GridColumn.MappingName == "OrderID") 
    { 
        var rowData = e.DataRow.RowData as OrderInfo; 
        if (rowData.OrderID > 20) 
            rowData.ProductName = "NewProduct"; 
    } 
} 
 
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 in Edit Mode. 
If you want to move the current cell to the first column on pressing the Tab key, you can use the KeyDown event. 

Code Sample: 
 
sfDataGrid.TableControl.KeyDown += TableControl_KeyDown; 
void TableControl_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Tab && sfDataGrid.IsAddNewRowIndex(sfDataGrid.CurrentCell.RowIndex) && sfDataGrid.CurrentCell.ColumnIndex == sfDataGrid.Columns.Count -1) 
    { 
        sfDataGrid.MoveToCurrentCell(new Syncfusion.WinForms.GridCommon.ScrollAxis.RowColumnIndex(sfDataGrid.CurrentCell.RowIndex, 0)); 
        e.Handled = true; 
    } 
} 
 


Arulraj A 



AV Avinash August 24, 2018 01:42 PM UTC

When used this sfDataGrid.View.Records.Add(e.NewObject); it is adding 2 rows  in sfDataGrid



AA Arulraj A Syncfusion Team August 29, 2018 11:59 AM UTC

 
Thanks for your update. 
 
We have analyzed this reported scenario. By default, when the focus is moved from AddNewRow to any other row, the AddNewRow value will be committed to the view. This is the expected behavior. Currently we don’t have support to cancel adding new row within the AddNewRowInitiating event. We have logged this requirement as a feature request in our database and we have planned to implement this in our 2018 Volume 3 release, which is expected to be rolled out in the end of September, 2018. 
 
Regards, 
Arulraj A 



AV Avinash September 7, 2018 06:14 AM UTC

When Adding New row is SFDataGrid, it doesn't scroll to row when it row reaches height of grid.
though it is adding row, but i have make entry blindly as i am not able to see row,


AA Arulraj A Syncfusion Team September 10, 2018 10:40 AM UTC

Thanks for your update. 
 
To scroll down once new row added is SFDataGrid and reaches the height of grid when new row goes beyond the height, we suggest you the following solutions. 
 
Solution 1: 
There is no default support to achieve your required scenario. If you want to scroll to the newly added row index with the default AddNewRow behavior, you need to handle the CurrentCellActivating and Records.CollectionChanged events with certain limitation as of following code sample, 
 
Code Sample: 
sfDataGrid1.CurrentCellActivating += sfDataGrid1_CurrentCellActivating; 
sfDataGrid1.View.Records.CollectionChanged += Records_CollectionChanged; 
sfDataGrid1.SelectionChanging +=sfDataGrid1_SelectionChanging;        
 
void sfDataGrid1_CurrentCellActivating(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellActivatingEventArgs e) 
{ 
    if (sfDataGrid1.IsAddNewRowIndex(sfDataGrid1.CurrentCell.RowIndex) && sfDataGrid1.View.IsAddingNew && e.DataRow.RowIndex == sfDataGrid1.GetAddNewRowIndex() + 1) 
    { 
        // To cancel the current cell moving to the start row index after adding the new row.  
        e.Cancel = true; 
    } 
} 
 
void sfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e) 
{ 
    if (sfDataGrid1.IsAddNewRowIndex(sfDataGrid1.CurrentCell.RowIndex) && sfDataGrid1.View.IsAddingNew) 
    { 
        e.Cancel = true; 
    } 
} 
 
void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
   if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) 
   { 
       // To scroll the grid to the newly added row position. 
       var scrollValue = this.sfDataGrid1.TableControl.ScrollRows.Distances.GetCumulatedDistanceAt(e.NewStartingIndex); 
       this.sfDataGrid1.TableControl.ScrollRows.ScrollInView(e.NewStartingIndex); 
       this.sfDataGrid1.TableControl.VerticalScroll.Value = (int)scrollValue; 
   } 
} 
 
 
 
 
 
Solution 2: 
By default, while adding the new row using the AddNewRow, it will be added to the end of the grid view. You can change this behavior to add the new item to top of the view by setting the NewItemPlaceholderPosition property as AtBeginning. Refer to the following code sample, 
 
Code Sample: 
// To insert the add new row at the top. 
sfDataGrid1.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning; 
 
 
 
Let us know if you need any further assistance on this. 
                                                                         
Regards, 
Arulraj A 



AV Avinash September 14, 2018 04:13 AM UTC

In last post for scrolling down you have mentioned there is some limitation what are those ?

Further after adding scroll down code in sfDataGrid, i am facing issue as mentioned in file attached.

Attachment: Untitled_f9cef91b.zip


MA Mohanram Anbukkarasu Syncfusion Team September 14, 2018 12:40 PM UTC

Hi Avinash, 
 
Thanks for your update. 
 
Queries 
Solution 
In last post for scrolling down you have mentioned there is some limitation what are those ? 
Please find the limitations of the provided workaround. 
  1. When AddNewRowPosition is Top and it is in editing, you can’t able to move the selection to the first row.
  2. The provided workaround will not work for grouping.
Further after adding scroll down code in sfDataGrid, I am facing issue as mentioned in file attached. 
 
We have checked the provided workaround  with AddNewRowPosition as FixedBottom as in the attached image. We were unable to reproduce it. Please provide more details about the issue. It will be helpful for us to provide exact solution for it. 
 
 
 
Regards, 
Mohanram A. 



AA Arulraj A Syncfusion Team September 18, 2018 10:18 AM UTC

 
We are glad to announce that our v16.3.0.17 beta release is rolled out, we have provided Support to “Cancel adding new row within the AddNewRowInitiating event” and it is available for download under the following link.  
   
  
We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance. 
 
Regards, 
Arulraj A 



AV Avinash December 11, 2018 11:20 AM UTC

[1] Need to move to first column of next row on row_validated event for below cases:
     [A] If row is available
     [B] If new row is added.

Further i have below me
ntioned code which is first moving to current row's first column & then it is moving to next row, my issue is that i have cell_endedit event on first column which reset's when move to current cell is used in RowValidated event,

Expecting reply asap.

SfDataGrid1.RowValidated += SfDataGrid1_RowValidated;
 
 
private void SfDataGrid1_RowValidated(object sender, RowValidatedEventArgs e) 
{ 
  
        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)); 
  
}


JN Jayaleshwari N Syncfusion Team December 12, 2018 11:24 AM UTC

Hi Avinash,  

We have prepared the sample based on the provided code snippet and we found that the row index is current cell is moved to first column of next row. Can you please confirm us, whether you are trying to report this case as issue?  

We have attached the tested sample and video for your reference and you can download the same from the following location. 

If your requirement is different from the above, can you please revert us with the modified sample and replication procedure/video illustration of the reported issue?. Also please let us know what you are trying to do with CurrentCellEndEdit event in order to investigate further to provide appropriate solution at earlier? .   

Regards,  
Jayaleshwari N 



AV Avinash December 13, 2018 07:05 AM UTC

In my case it is also moving to next row but before that Cell_EndEdit event occurs of previous row's-first column & because of that my previous row get's reset as i have some code on endedit event.
Kindly check once.


AV Avinash December 14, 2018 09:47 AM UTC

Any update ???


JN Jayaleshwari N Syncfusion Team December 14, 2018 12:16 PM UTC

Hi Avinash, 
 
Thanks for the update. 
 
The query you have reported is the actual behavior of the of the DataGrid. The workflow is the row will be validated first, then the current cell will end edit and then the selection will be moved.  
It is hard to trace why this issue is arising without having a look at the sample. So we would request you to share the code snippet you are using in the CurrentCellEndEdit event which would be better to provide you the appropriate solution. 
 
Regards, 
Jayaleshwari N 


Loader.
Up arrow icon