How to sort a DataGridView that is bound to a collection of custom objects?
Asked Answered
E

1

5

So I have been following this guide for data binding on Windows Forms controls (MAD props to the author, this guide is great), and I have used this to create a custom class and bind a DataGridView to a collection of this class:

class CompleteJobListEntry
{
    private string _jobName;
    private Image _jobStatus;
    private DateTime _jobAddedDate;
    private string _jobAddedScanner;
    private string _jobAddedUser;
    private string _jobLastActivity;
    private DateTime _jobActivityDate;
    private string _jobActivityUser;

    public string JobName { get { return _jobName; } set { this._jobName = value; } }
    public Image JobStatus { get { return _jobStatus; } set { this._jobStatus = value; } }
    public DateTime JobAddedDate { get { return _jobAddedDate; } set { this._jobAddedDate = value; } }
    public string JobAddedScanner { get { return _jobAddedScanner; } set { this._jobAddedScanner = value; } }
    public string JobAddedUser { get { return _jobAddedUser; } set { this._jobAddedUser = value; } }
    public string JobLastActivity { get { return _jobLastActivity; } set { this._jobLastActivity = value; } }
    public DateTime JobActivityDate { get { return _jobActivityDate; } set { this._jobActivityDate = value; } }
    public string JobActivityUser { get { return _jobActivityUser; } set { this._jobActivityUser = value; } }
}

At this point, I import a bunch of data from various SQL databases to populate the table, and it turns out great. The guide even provides an excellent starting point for adding filters, which I intend to follow a bit later. For now, though, I am stuck on the sorting of my newly generated DataGridView. Looking around, I've discovered that the DataGridView has its own Sort method, usable like:

completeJobListGridView.Sort(completeJobListGridView.Columns["JobName"], ListSortDirection.Ascending);

However, when I try to do this, I get an InvalidOperationException that tells me "DataGridView control cannot be sorted if it is bound to an IBindingList that does not support sorting." I've found both the IBindingList and IBindingListView interfaces, but making my class inherit either of these interfaces doesn't solve the problem.

How do I do this? I am completely stuck here...

Ess answered 27/6, 2012 at 23:20 Comment(4)
There are lots of these available from Google, for example by searching "C# sortable binding list". Typically you inherit from BindingList<T> and implement SupportsSortingCore.Petticoat
This works, I guess. My main problem was that I wasn't searching with the right phrases :P Just gonna leave this link here in case anyone else wants to see it. codeproject.com/Articles/31418/…Ess
An alternative solution is documented here : https://mcmap.net/q/588893/-datagridview-sort-and-e-g-bindinglist-lt-t-gt-in-net.Eel
An alternative solution is documented here : https://mcmap.net/q/588893/-datagridview-sort-and-e-g-bindinglist-lt-t-gt-in-net.Eel
H
1

If your data is in a collection, you should be able to use the BindingListView library to easily add sorting capabilities to your DGV. See How do I implement automatic sorting of DataGridView? and my answer to How to Sort WinForms DataGridView bound to EF EntityCollection<T> for more information and code snippets.

Haldan answered 30/7, 2013 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.