Exception when try to add a new row, after another row was deleted (with a DataTable as ItemsSource)

Hello everyone,

I attach an example that reproduce the issue i'm experiencing with the SfDataGrid component.

I have a DataTable and the user need to edit, add and remove rows from the datagrid. I use a context menu for deleting rows. I need that the RowStatus for the Rows in the datagrid is correctly updated (for add, edit and remove state).

In the attached example, when the user try to add a new row, and double click to the cell for edit it, it work fine.

But if the user try to delete a row, then add a new row and double click over the cell for edit it, it get an exception of index out of bounds.

I tried 3 way for delete the record:

  • Test 1 (row 33): it delete the record, the record disappear from the grid, the record is marked as Deleted in the RowState. It generate an exception if the user try to add a new row
  • Test 2  (row 36): it delete the record, the record disappear from the grid, the record is marked as Deleted in the RowState. It generate an exception if the user try to add a new row
  • Test 3  (row 39): it delete the record, the record disappear from the grid, the record is NOT marked as Deleted in the RowState. It DOESN'T generate an exception if the user try to add a new row

How can i solve the issue?

Thanks

Sandro


Attachment: TestDeleteDataGridSyncfusion_50a7bfc8.zip

1 Reply

MA Mohanram Anbukkarasu Syncfusion Team July 21, 2021 06:04 AM UTC

Hi Sandro Cavazzoni, 

Thanks for contacting Syncfusion support.  

We have checked the provided sample. You can resolve the reported issue by deleting the row from the DataTable as shown in the following code example.  

Code example :  

private void MyDataGrid_GridContextMenuOpening(object sender, GridContextMenuEventArgs e) 
{ 
    if (e.ContextMenuType == ContextMenuType.RecordCell) 
    { 
        MyDataGrid.RecordContextMenu.Items.Clear(); 
        var menuInfo = e.ContextMenuInfo as GridRecordContextMenuInfo; 
        if (menuInfo != null) 
        { 
            var item = new MenuItem { Header = "Delete row" }; 
            item.Click += delegate 
            { 
                var dataRowView = menuInfo.Record as DataRowView; 
 
                (MyDataGrid.ItemsSource as DataTable).Rows.Remove(dataRowView.Row); 
 
            }; 
            MyDataGrid.RecordContextMenu.Items.Add(item); 
        } 
    } 
} 



Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



Loader.
Up arrow icon