所以我一直关注this guide for data binding on Windows Forms controls(MAD道具给作者,这个指南很棒),我用它来创建一个自定义类并将DataGridView
绑定到这个类的集合:
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; } }
}
此时,我从各种SQL数据库导入一堆数据来填充表格,结果很棒。该指南甚至为添加过滤器提供了一个很好的起点,我打算稍后介绍一下。但是现在,我仍然坚持对新生成的DataGridView
进行排序。环顾四周,我发现DataGridView
有自己的Sort方法,可以使用:
completeJobListGridView.Sort(completeJobListGridView.Columns["JobName"], ListSortDirection.Ascending);
但是,当我尝试这样做时,我得到一个InvalidOperationException
告诉我“如果DataGridView控件绑定到不支持排序的IBindingList,则无法对其进行排序。”我已经找到了IBindingList和IBindingListView接口,但是让我的类继承其中任何一个接口都无法解决问题。
我该怎么做?我完全被困在这里......
答案 0 :(得分:1)
如果您的数据在一个集合中,您应该能够使用BindingListView
library轻松地为您的DGV添加排序功能。有关详细信息和代码段,请参阅How do I implement automatic sorting of DataGridView?和我对How to Sort WinForms DataGridView bound to EF EntityCollection<T>的回答。