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

Deleting new row

I've looked at your advice to someone else about deleting a new row and refreshing the sfdatagrid. I've converted your code to Visual Basic as fiollows:
Private Sub sfDataGrid_CurrentCellKeyDown(ByVal sender As Object, ByVal e As CurrentCellKeyEventArgs)
    If Me.sfDataGrid.IsAddNewRowIndex(Me.sfDataGrid.CurrentCell.RowIndex) AndAlso e.KeyEventArgs.KeyCode = Keys.Escape AndAlso sfDataGrid.View.CurrentAddItem IsNot Nothing Then
        If Me.sfDataGrid.CurrentCell.IsEditing Then Me.sfDataGrid.CurrentCell.EndEdit(True)
        Dim gridModel = ReflectionHelper.GetProperty(GetType(SfDataGrid), "GridModel").GetValue(sfDataGrid, Nothing)
        Dim addNewRowController = ReflectionHelper.GetProperty(gridModel.[GetType](), "AddNewRowController").GetValue(gridModel, Nothing)
        Dim cancelAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.[GetType](), "CancelAddNew")
        ReflectionHelper.Invoke(cancelAddNewMethod, addNewRowController, New Object() {})
    End If
End Sub

I keep getting compiler errors on ReflectionHelper. Can you please tell me what namespace this class is in? Looking at Microsoft's website I see this is no longer supported and I can't find the namespace they quote. I need to include the namespace in my form.

3 Replies

SA Saravanan Ayyanar Syncfusion Team January 13, 2020 12:48 PM UTC

Hi Tom, 
 
Thank you for using Syncfusion  controls. 
 
We have checked your provided code example. You can use the type ReflectionHelper from the Syncfusion.WinForms.Core.Utils namespace. Please refer the below code snippet. 
 
Imports Syncfusion.WinForms.Core.Utils 
 
Private Sub SfDataGrid1_CurrentCellKeyDown(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs) 
        If Me.sfDataGrid1.IsAddNewRowIndex(Me.sfDataGrid1.CurrentCell.RowIndex) AndAlso e.KeyEventArgs.KeyCode = Keys.Escape AndAlso sfDataGrid1.View.CurrentAddItem IsNot Nothing Then 
              If Me.sfDataGrid1.CurrentCell.IsEditing Then 
                    Me.sfDataGrid1.CurrentCell.EndEdit(True) 
              End If 
              Dim gridModel = ReflectionHelper.GetProperty(GetType(SfDataGrid), "GridModel").GetValue(sfDataGrid1, Nothing) 
              Dim addNewRowController = ReflectionHelper.GetProperty(gridModel.GetType(), "AddNewRowController").GetValue(gridModel, Nothing) 
              Dim cancelAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.GetType(), "CancelAddNew") 
              ReflectionHelper.Invoke(cancelAddNewMethod, addNewRowController, New Object() { }) 
 
        End If 
 
 
Please let us know, if you require further assistance on this. 
 
Regards, 
Saravanan A. 



TK Tom Krieg January 14, 2020 06:04 AM UTC

Thank you for that. The code runs through without error and I checked with the debugger and everything executes properly. Problem is ... the new line is still visible.
Here's the scenario ... empty sfdatagrid. Allow add new row = true, the add new row text = "Click to add new row" and the new row position is at the end of exisitng data.

User clicks "Add new row" and the new row is created and focus is on the first cell.
User starts typing and realises he has made a right royal mess of the whole line and wants to cancel.
User presses ESCape key.
The code executes and the new row is cancelled.
The new row is still shown, but is blank.

Can the new row just "disappear" and the "Click here..." text shown instead?

The user has to up-arrow to get rid of the new row.


VS Vijayarasan Sivanandham Syncfusion Team January 15, 2020 12:38 PM UTC

Hi  Tom Krieg, 
 
Thanks for the update. 
 
We have replicated your scenario based on the provided information. And the addnew row text is not shown since the selection is maintained in that row. However, you can make the addnewrow text visible, by clearing selection as like below code snippet, 
  
Code snippet:   
  
 
Imports Syncfusion.WinForms.Core.Utils
 
Private Sub SfDataGrid1_CurrentCellKeyDown(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs) 
        If Me.sfDataGrid1.IsAddNewRowIndex(Me.sfDataGrid1.CurrentCell.RowIndex) AndAlso e.KeyEventArgs.KeyCode = Keys.Escape AndAlso sfDataGrid1.View.CurrentAddItem IsNot Nothing Then 
              If Me.sfDataGrid1.CurrentCell.IsEditing Then 
                    Me.sfDataGrid1.CurrentCell.EndEdit(True) 
              End If 
              Dim gridModel = ReflectionHelper.GetProperty(GetType(SfDataGrid), "GridModel").GetValue(sfDataGrid1, Nothing) 
              Dim addNewRowController = ReflectionHelper.GetProperty(gridModel.GetType(), "AddNewRowController").GetValue(gridModel, Nothing) 
              Dim cancelAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.GetType(), "CancelAddNew") 
              ReflectionHelper.Invoke(cancelAddNewMethod, addNewRowController, New Object() { })
                            this.sfDataGrid.ClearSelection();   
        End If 
 
 
Please let us know, if you need any further assistance on this.
 
Regards, 
Vijayarasan 


Loader.
Up arrow icon