This is a migrated thread and some comments may be shown as answers.

Delete a row of RadGridView in the event RowsChanging

1 Answer 645 Views
GridView
This is a migrated thread and some comments may be shown as answers.
will
Top achievements
Rank 1
will asked on 28 Aug 2019, 07:26 PM

Hello, please i need your help,

 

I have a RadGridView and the rows have different types, for example: Type A, Type B, .... and sometimes there is a relation between rows, and if a user try to delete a row (for example row A) I check if there is a relation between the row that the user want to delete (row A) and all the rows of the RadGridView, and if there is a relation I propose to the user if he want to delete the row (Row B for example) (or rows) who the row A have a relation.


I used the event RowsChanging to get the index of the rows that the user wants to delete, like this, and then I can delete the rows who have a relation with the row A.

 

private void radGridView_RowsChanging(object sender, GridViewCollectionChangingEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Remove)  // The Action is to delet the row A
            {

              //  if(I check if row A have a relation with another row)

             {

             //i delet this rows

        radGridView .Rows.RemoveAt(indexoftherowiwanttodelet);

             }         

      }

}

 

My problem is:


If the index of the row A is smaller than the index of the row B, the code works very well.
But if the index of the row A is bigger than the row B, I have an exception: 

'System. ArgumentOutOfRangeException' occurred in mscorlib.dll

 

I need your help and thank you.

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Aug 2019, 02:02 PM
Hello, Will,  

Note that the RowsChanging event can be cancelled if the delete operation is not allowed. This means that the specific row is not deleted yet. But in the event handler you call the RadGridView.Rows.RemoveAt method passing the index of the row that you want to delete. Calling this method would delete the row triggering once again the RowsChanging event. Have in mind two important things: unsubscribe from the event before manipulating the Rows collection in order to avoid multiple firing of the event and subscribe to it again after the Rows.RemoveAt method call. The other thing to consider is that when you call the RemoveAt method (deleting Row B) the index of the row that is currently being processed (Row A) in the RowsChanging event may be changed if Row B is before Row A: for example index 0 - Row B, index 1 - Row A, index 2 - Row C. If you delete Row B, Row A's index becomes 0, not 1 as it used to be. That is why it is not recommended to manipulate the Rows collection (add/delete rows) while a delete operation is being performed. 

The RowsChanged event is fired once the delete operation is completed. Feel free to use this event for any manipulations of the collection. But feel free to use the RowsChanging event for calculating the proper index considering whether the affected row is before or after the related rows and adjust the index +/-1.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
will
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or