views:

2554

answers:

5

Hi there

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!

+1  A: 

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.

Lennaert
A: 

You could implement jQuery client side sorting and let it handle it.

http://docs.jquery.com/UI/Sortables/sortable

brendan
+1  A: 

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.

Matthew Talbert
When the query data is coming from an in-memory ICollection, this works to get data into the DataGridView, but the column headers still aren't sortable. To make them sortable, you need to do something like Brian describes with SortableBindingList.
Eric
+1  A: 

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.

    //I know that you asked the question in VB.NET but I don't know the syntax that well.
    public class SortableBindingList<T> : BindingList<T>
    {
         //override necessary sort core methods
    }

    SortableBindingList<string> list = new SortableBindingList<string>(QueryReOrder.ToList());

    //use list as your DataSource now
Brian ONeil
A: 

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.

Eric