I’m struggling with DataGridView and the sorting feature. I have attached a solution that demonstrate this.
If you run it and click on the button “Get data list” and then try to sort the list you will get an error. The reason is that when the list is sorted the grid is physically swapping data in records in the databound list. I think this is unfortunate.
First, The list in the sample is readonly. Therefore the error message.
Second, If you sort a list, that is not readonly, it will work. But then the underlying data think that it has been changed and needs to be saved to the database. So every line is saved even if the user changed none but just sorted the list for some reason.
Also there is some issues if a list is bound to more than 1 grid. See attached picture. I’m maybe stretching it here, but still an issue :-).
I’m thinking the grid should handle the sorting itself and not rely on any sorting feature of the underlying list.
we had to refine the fix for #2412 making that behavior the non default behavior because it caused some other issues.
The next Wisej build will include a new property AllowSortingDataSource (default = true) that you can set to false
to get the desired behavior.
I will inform you when the new build is available.
Thanks for explaining this.
A DataGridView that can handle sorting of any bound list by keeping a sorted reference list to the records in the original list. Sorting is done on the reference list and not the bound list.
I know sorting can be handled in the business object bound list by adding logic to the list. But I think this kind of sorting is a UI thing and should be handled by the UI framework.
Logged as #2412 Sorting a DataGridView bound to a read-only IList shouldn’t sort the data source.
It will behave like it does closer to winforms: ignore the sorting on IsReadOnly IList and ignore it on IBindingList that don’t support sorting. If does programmatically by the it will throw an exception.
I logged it internally as checking if the list IsReadOnly = true and in that case ignore the sorting or throw an exception (we haven’t decided yet, suggestions?)
In your test I always get IsReadyOnly = false on all lists so this wouldn’t fix the problem in that case.
In general, it is impossible to sort a list without moving the items in it.
A data-storage bound list should support sorting to it can take over the sorting and update the grid back, or ignore it. A normal list can be sorted without problems.
Otherwise the only other option is to disable sorting, use custom sorting, which can be done by implementing SupportsSorting on a IBindingList object or overriding the Sort() method.
Did you log the sorting a readonly list bug? Any Update?
How about the suggestion in my previous post:
“about adding a property to DataGridView to force Wisej to always use it’s own sorting implementation?”
Sorting a list by physically swapping records is very bad as all the records is marked as updated and needs to be saved to the database as mentioned in the first post. I still have no solution for this except for disabling the sort on the grid.
It is the list down on the left that is readonly. It is populated when you click on the “Get data list” button.
I’m trying to move our WinForms application over to Wisej. It is heavily based on databinding to objects. I dont’t believe in connecting data directly. So for that we use the CSLA framework. It supports every kind of databinding, like WinForms and WPF.
You mentioned that Wisej also has implemented sorting if the source list don’t support sorting. How about adding a property to DataGridView to force Wisej to always use it’s own implementation? And eliminate all the problems I have.
We use DevExpress controls in the WinForms application. I think their grid always use its own sorting. Maintaining a reference sort list to the original list.
The DataGridView sorts its rows when it’s not databound. When it’s databound it must sort the data source since the data it’s there and the rows must match. Another option is to load from a data source unidirectionally and don’t keep the data binding using the new Fill() or Append().
When the data source supports sorting, the sorting is delegated to the source. For example, a connected data source can sort using the database ORDER BY, or LINQ, or a custom implementation. Data binding in Wisej is based on WinForms databing and is a lot more sophisticated than simple mono directional data binding.
Also sorting in the DataGridView adds another layer of flexible implementation. You can use the built-in quick sort/heap sort combination, or pass your own comparer to achieve multi column sorting, for example.
When data bound, and sorting is automatic, the DataGridView checks if the data source is an IBindingList and if it SupportSorting. If it does then sorting is delegated to the data source implementation, which is outside of Wisej scope: it calls the ApplySort method and updates the grid.
I think the issue that you uncovered is with read only lists. In Winforms if you try to automatically sort a data source that is not an IBindingList that supports sorting you get an exception that sorting a data bound grid is not supported. In Wisej we implemented also sorting of source lists that don’t support sorting. However, when the list is read only it obviously shouldn’t re-arrange the rows. Will log as a bug.
In any case, the result would be either an exception that sorting is not supported or you have to set sorting to Programmatic and handle it in your app.
Please login first to submit.