views:

413

answers:

2

Hi there!

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!

A: 

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

robber.baron
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
A: 

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!

Shalan