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

Deletion/Cancelling of New Row

Hey,
I have a SfDataGrid where the user has the option to add a new row. 
Question ... when the user is entering information in the new row is there an option for the user to cancel/delete the new row before it is committed to the grid by hitting Delete or Esc or something along those lines?

Thanks

7 Replies

FP Farjana Parveen Ayubb Syncfusion Team July 26, 2019 07:30 AM UTC

Hi Hemraj, 
 
Thank you for using Syncfusion controls. 
 
We have analyzed your query, when entering the Esc key in AddNewRow the new row is not committed in SfDataGrid this can be achieve by using CancelNew method in SfDataGrid.View. 
 
Please refer the below code example 
 
sfDataGrid.CurrentCellKeyDown +=sfDataGrid_CurrentCellKeyDown; 
 
void sfDataGrid_CurrentCellKeyDown(object sender, CurrentCellKeyEventArgs e) 
{ 
    if(this.sfDataGrid.IsAddNewRowIndex(this.sfDataGrid.CurrentCell.RowIndex) && e.KeyEventArgs.KeyCode == Keys.Escape) 
        this.sfDataGrid.View.CancelNew(); 
} 
 
 
 
Regards,
Farjana Parveen A
 



HR Hemraj Ramnarine July 26, 2019 01:00 PM UTC

Hey Farjana,
When I test the sample provided I'm getting the following error on the line "this.sfDataGrid.View.CancelNew(); ": CurrentAddItem should not be null to perform this operation.
The error also occurs when I try the update in my code.
Any suggestions?

Regards,
Hemraj



FP Farjana Parveen Ayubb Syncfusion Team July 29, 2019 07:03 AM UTC

Hi Hemraj, 
 
Sorry for the inconvenience caused. 
 
We have analyzed your query, when you enter the edit mode in AddNewRow control the added item is created and we can add the new item in SfDataGrid, so we try to cancel the added item before its created. So we have to consider the added item also. 
 
Please refer the below code example 
void sfDataGrid_CurrentCellKeyDown(object sender, CurrentCellKeyEventArgs e) 
{ 
    if(this.sfDataGrid.IsAddNewRowIndex(this.sfDataGrid.CurrentCell.RowIndex) && e.KeyEventArgs.KeyCode == Keys.Escape && sfDataGrid.View.CurrentAddItem != null ) 
        this.sfDataGrid.View.CancelNew(); 
} 
 
 
 
Regards, 
Farjana Parveen A 



HR Hemraj Ramnarine July 29, 2019 09:40 PM UTC

Hey Farjana,
The adding of sfDataGrid.View.CurrentAddItem works but the grid does not automatically refresh. I had to introduce a sfDataGrid.MoveToCurrentCell call in order for the grid to automatically refresh. 
Question is there another way to refresh the grid UI after calling sfDataGrid.View.CancelNew() to not show the new row?

Regards,
Hemraj



FP Farjana Parveen Ayubb Syncfusion Team July 30, 2019 12:58 PM UTC

Hi Hemraj, 
 
Thank you for your update. 
 
We have analyzed your query, we can cancel the add new row operations when Esc key is pressed by using CancelAddNew method in AddNewRowController and it will refresh the SfDataGrid also. 
 
Please refer the below code example 
 
void sfDataGrid_CurrentCellKeyDown(object sender, CurrentCellKeyEventArgs e) 
{ 
    if (this.sfDataGrid.IsAddNewRowIndex(this.sfDataGrid.CurrentCell.RowIndex) && e.KeyEventArgs.KeyCode == Keys.Escape && sfDataGrid.View.CurrentAddItem != null) 
    { 
        if (this.sfDataGrid.CurrentCell.IsEditing) 
            this.sfDataGrid.CurrentCell.EndEdit(true); 
        var gridModel = ReflectionHelper.GetProperty(typeof(SfDataGrid), "GridModel").GetValue(sfDataGrid, null); 
        var addNewRowController = ReflectionHelper.GetProperty(gridModel.GetType(), "AddNewRowController").GetValue(gridModel, null); 
        var cancelAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.GetType(), "CancelAddNew"); 
        ReflectionHelper.Invoke(cancelAddNewMethod, addNewRowController, new object[] { }); 
    } 
} 
 
 
Regards, 
Farjana Parveen A 



HR Hemraj Ramnarine July 31, 2019 07:24 PM UTC

All set.
Thanks, Farjana.


FP Farjana Parveen Ayubb Syncfusion Team August 1, 2019 05:02 AM UTC

Hi Hemraj 
 
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, 
Farjana Parveen A 


Loader.
Live Chat Icon For mobile
Up arrow icon