I use the AddingDataRow event to add an event hanlder to each new cell in the row. I’d like to remove this event handler when the row is removed as I believe not doing so would cause the cell object to be leaked. However I can’t find a RemovingDataRow event or similar, does such an event exist?
I could add code to remove the events everytime a row is explicitly removed but this feels quite a bit harder than just using a event.
Imported from legacy forums. Posted by Robert (had 469 views)
There will be no memory leak as long as the lifespan of the object containing the callback method is longer than the lifespan of the object exposing the event. That is, if you subscribe to an event on a cell, and the event handler (the callback) is an instance method of the form on which the grid is located, then when deleting the cell, it will not leak since the object exposing the event (the cell) has a shorter lifespan than the object containing the event handler (the form).
BTW, do you really need to subscribe to cell events in the AddingDataRow event? You can use the DataRowTemplate to do this once, when the grid is first initialized.
foreach( Cell cell in dataRowTemplate1.Cells )
new EventHandler( cell_ValueChanged );
Imported from legacy forums. Posted by André (had 658 views)