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
SIGN IN To post a reply.
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();
}
|
Sample Location: https://www.syncfusion.com/downloads/support/forum/146202/ze/AddNewRow_Cancel960045809
Regards,
Farjana Parveen A
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();
} |
Sample Location: https://www.syncfusion.com/downloads/support/forum/146202/ze/AddNewRow_Cancel-749228332
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[] { });
}
} |
Sample Location: https://www.syncfusion.com/downloads/support/forum/146202/ze/AddNewRow_Cancel2032657872
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
SIGN IN To post a reply.
- 7 Replies
- 2 Participants
-
HR Hemraj Ramnarine
- Jul 25, 2019 12:19 PM UTC
- Aug 1, 2019 05:02 AM UTC