winforms datagridview sorting using LINQ

Im using Linq...more specifically, PLINQO. anyway the following is an example of a query I have bound to a datagridview (winforms):

    public static List<Task> GetUserTasks( Guid userID ) {
        using (myDataContext ctx = new myDataContext()) {
            try {
                return ctx.Manager.Task.GetByUserID( userID ).ToList();
            } catch (Exception) {
                throw;
            }
        }
    }

In my UI, I have the following setup to bind:

        BindingSource bs = new BindingSource();
        bs.DataSource = DUtasks.GetUserTasks( User.Current.UserID );
        dgvTasks.DataSource = bs;

It works, but no sorting is possible. I tried "AsEnumerable()" instead of "ToList()" but that for some reason, throws an "objection reference" error. Any ideas as to how I can proceed on this front?

Many thanks!

13.10.2009 15:34:46
3 ОТВЕТА

Have you tried returning an IOrderedEnumerable? Not sure if it will help but that is designed to handle ordered LINQ results.

0
13.10.2009 15:47:24
HI robber. No unfortunately that does not work. I already have tried an "orderby" appended to the query with no result. I got the sort property in each column set to automatic (oh, btw, I hjave chosen not to autogenerate columns, if that makes a diff.), so I know that as soon as I have it right the SortGlyph is supposed to appear, right?
Shalan 13.10.2009 16:57:51
РЕШЕНИЕ

OK, problem sorted!!! :)

found the following link: SortableBindingList... (my comment is at the bottom with converted C# working code).

Now all my query methods that return List<(ENTITY)> simply get used like this:

        SortableBindingList<Task> sortedTasks = new SortableBindingList<Task>( DUtasks.GetUserTasks( User.Current.UserID ) );
        dgvTasks.DataSource = sortedTasks;
        dgvTasks.Sort( colTaskDue, ListSortDirection.Ascending );

Hope this helps someone!

1
14.10.2009 16:19:37

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.

0
6.08.2013 08:08:24