GridViewCellInfo.Item is null on SelectedCellsChanged

1 Answer 19 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 13 Jun 2022, 12:30 PM

Hello, I'm in front of a mystery.

I have 2 columns, created dynamically in a RadGridView. Let's name them column A and column B.

When I have one row, and I select a cell in column A, then click on column B's header to sort it, SelectedCellsChanged is fired and args.RemovedCells contains my cell. From this, I understand that I lose my selection when I sort a column, which is perfectly fine.

When I have two rows, and I select both cells in column A, then click on column B's header to sort it, both GridViewCellInfo have their .Item to null in SelectedCellsChangedEventArgs.RemovedCells, which is unexpected on my side!

That way, I can't identify the rows to which these removed cells belong, which is painful.

Why is that?
Thanks!

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 15 Jun 2022, 03:01 PM

Hello Eldoir,

The reason for this behavior is that once the Items collection of the control is reset (which is the case when it is sorted) the selection is cleared and a collection of cell regions containing the previously selected cells is passed. A cell region is a rectangular region with a Left, Top, Width, and Height properties and in the first scenario you described would have the 4 properties assigned to 0, 0, 1, and 1, respectively.

When the sorting is performed, however, this region no longer corresponds to the same cell (in most cases) as it is called with the new order of the items. That said, I do not believe we can handle this differently in this particular scenario.

With this in mind, what I can suggest would be to persist the SelectedCells collection in the Sorting event handler and then use this persisted value as required in the SelectedCellsChanged event. You can additionally set a flag in the Sorting event to conditionally perform any extra steps required by the persistence.

Please give this a try and let me know if such an approach would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or