Allow user to sort columns from a LINQ query in a DataGridView
Asked Answered
G

6

8

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!

Greenling answered 28/1, 2009 at 14:44 Comment(0)
H
5

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
Hysell answered 11/5, 2009 at 3:3 Comment(0)
T
3

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.

Transfigure answered 4/2, 2009 at 13:30 Comment(0)
G
2

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.

Gavriella answered 12/3, 2009 at 17:28 Comment(1)
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.Haematogenesis
M
2

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.

var query = from o in m_ds.Objective

        join ot in m_ds.ObjectiveType on o.ObjectiveTypeId equals ot.Id
        join dst in m_ds.DevelopmentStatusType on o.DevelopmentStatusTypeId equals dst.Id
        join rt in m_ds.ResultType on o.PrecedenceResultTypeId equals rt.Id

        select new
        {
            o.Id,
            type = ot.Description,
            precedence = rt.Description,
            o.Symbol,
            o.Title,
        };

2) Convert that IEnumerable result set to a DataTable!

public static DataTable DataTableFromIEnumerable( IEnumerable ien )
{
    DataTable dt = new DataTable();
    foreach ( object obj in ien )
    {
        Type t = obj.GetType();
        PropertyInfo[] pis = t.GetProperties();
        if ( dt.Columns.Count == 0 )
        {
            foreach ( PropertyInfo pi in pis )
            {
                dt.Columns.Add( pi.Name, pi.PropertyType );
            }
        }

        DataRow dr = dt.NewRow();
        foreach ( PropertyInfo pi in pis )
        {
            object value = pi.GetValue( obj, null );
            dr[ pi.Name ] = value;
        }

        dt.Rows.Add( dr );
    }

    return dt;
}

3) Bind your DataGridView to that generic DataTable object.

var query = SqlHelper.GetFilteredObjective();
var bs = new BindingSource();
bs.DataSource = Utils.DataTableFromIEnumerable( query );
dgvObjectives.DataSource = bs;

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

Menon answered 17/11, 2010 at 20:29 Comment(0)
H
1

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.

Haematogenesis answered 14/12, 2009 at 18:41 Comment(0)
L
1

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.

Landers answered 6/8, 2013 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.