I can't quite work out how to allow a DataGridView populated at runtime to sort (when users click on the column headers) where a LINQ from XML query is the DataSource, via a BindingSource.
Dim QueryReOrder = From Q In Query _
Where ((0 - Q.Qualifier) / cmbTSStakeValue.Text) <= 0.1 _
Order By Q.Qualifier Descending _
Select Q
Dim bs As New BindingSource
bs.DataSource = QueryReOrder
DGFindMatch.DataSource = bs
Some of the DataGridView's properties are:
Sort Nothing String
SortProperty Nothing System.ComponentModel.PropertyDescriptor
SupportsAdvancedSorting False Boolean
SupportsChangeNotification True Boolean
SupportsFiltering False Boolean
SupportsSearching False Boolean
SupportsSorting False Boolean
Is there a simple solution that will allow a user to be able to sort these values by clicking the column header?
Thanks!
Ya, so I struggled with this for awhile. All the same answers about creating a custom generic IBindingList for each class. That's a crazy amount of work to do if the columns in your grid views are not static. I want to be able to change my linq queries and not have to change or update a class that implements the custom IBindingList. So, here's what I did:
1) Get your IEnumerable query.
2) Convert that IEnumerable result set to a DataTable!
3) Bind your DataGridView to that generic DataTable object.
4) That's it. One utility function and you're friggin' done :)
Props to Alberto Poblacion who wrote the above function to go from an IEnumerable to a DataTable: function thread
c# datagridview sortable linq to ADO.NET
You need to get the results of the LINQ query into something supports sorting functionality. This is typically done by deriving a class from BindingList and implementing the Sorting Core functionality in the derived class.
There are many examples of implementations out there to choose from and it is a pretty straight forward thing to implement. Here is an example of doing it on MSDN.
Once you have this implemented all you have to do is put your results in it and use it as your DataSource and the Grid should allow users to sort using the columns.
Another link that gives a complete example of how to construct a SortableBindingList, as described in Brian ONeil's answer, can be found here:
Sortable Binding List for custom data objects
I was able to use this example almost verbatim.
You need to get the query results as AsEnumerable().
Dim QueryReOrder = (From Q In Query _ Where ((0 - Q.Qualifier) / cmbTSStakeValue.Text) <= 0.1 _ Order By Q.Qualifier Descending _ Select Q).AsEnumerable()
I should mention I'm usually in C# so it's possible you'll have to vary the syntax slightly.
use only MySortableBindingList class in this page Implementing-a-Sortable-BindingList-
then
var yourLinqList = ...;
MySortableBindingList sortList = new MySortableBindingList(yourLinqList);
dataGridView1.DataSource = sortList;
then your dataGridView must be sort when cell header click.
My default approach is to copy everything into a DataTable and bind the DataGridView to that.
Obviously that won't work well if you want to add paging.