winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Enhance DataGridView to support non-destructive (multi column) Sort

Open coderb opened this issue 4 years ago • 5 comments
trafficstars

DataGridView.Sort seems to be implemented via the quick sort algo in DataGridViewRowCollection.RowArrayList.

I would like to be able to sort the DataGridView in a non-destructive (stable) way when changing the sort column (clicking the header to sort). Eg. If I sort on the first column and then sort on the second, the rows remain sorted by column 1 for equal values of column 2.

coderb avatar Jul 02 '21 14:07 coderb

@RussKie could you please transfer?

@coderb not sure if this will help but it sounds similar to your problem https://social.msdn.microsoft.com/Forums/vstudio/en-US/8fc635e2-9994-4214-9c89-43b3acf528e1/c-sorting-a-datagridview-by-one-column-then-by-another?forum=winformsdatacontrols

krwq avatar Jul 06 '21 06:07 krwq

@krwq nope, that link is a no go. i need to be able to (continue to) use the column headers to do the sorting. i've tried everything i could find on the web/stack overflow and even tried doing some reflection to SortInternal when overriding the Sort() method of DataGridView but it doesn't like to be passed both a column and comparer object.

it seems like some code support in DataGridView is appropriate.

coderb avatar Jul 06 '21 12:07 coderb

i've requested that the dotnet core project add a stable sort algorithm. dotnet/runtime#67546

coderb avatar Apr 04 '22 16:04 coderb

@corerb DataGridViewRowCollection.RowArrayList has its own custom sort implementation: https://github.com/dotnet/winforms/blob/d46ad2e8dc76248739d9ae22b28b399a6a6b299e/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewRowCollection.RowArrayList.cs#L35 . Unless the implementation is refactored to use the sort implementation from the core libraries, you would not be able to use stable sort even if it was added to the core libraries.

Also, it should be fairly straightforward to modify the compare function to take the current order into account. It may take a bit extra memory (e.g. temporary array of integers). It is negliable given how much memory is occupied by each row.

jkotas avatar Apr 09 '22 04:04 jkotas

This issue is now marked as "up for grabs", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 120 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

ghost avatar May 05 '22 02:05 ghost

@KlausLoeffelmann this sounds like a useful feature, thoughts?

JeremyKuhne avatar Aug 16 '23 20:08 JeremyKuhne

Hmm. I am a bit torn, also since Data Binding might be an issue here, since - if I remember correctly - sorting will then be delegated to a BindingList<T> and not be done by the DataGridView? But - I would need to refresh my memory what exactly is happening.

For unbound data sources, I would think implementing a MultiColumnSorter (based on IComparer) to pass to a DataGridView's Sort method should work already.

So, I don't know. Am I missing something else which should justify this feature?

KlausLoeffelmann avatar Aug 17 '23 16:08 KlausLoeffelmann