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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Deletion/Cancelling of New Row

Thread ID:

Created:

Updated:

Platform:

Replies:

146202 Jul 25,2019 12:19 PM UTC Aug 1,2019 05:02 AM UTC WinForms 7
loading
Tags: SfDataGrid
Hemraj Ramnarine
Asked On July 25, 2019 12:19 PM UTC

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

Farjana Parveen Ayubb [Syncfusion]
Replied On 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
 


Hemraj Ramnarine
Replied On 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


Farjana Parveen Ayubb [Syncfusion]
Replied On 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 


Hemraj Ramnarine
Replied On 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


Farjana Parveen Ayubb [Syncfusion]
Replied On 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 


Hemraj Ramnarine
Replied On July 31, 2019 07:24 PM UTC

All set.
Thanks, Farjana.

Farjana Parveen Ayubb [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon