如何排序绑定到自定义对象的集合中的DataGridView? [英] How to sort a DataGridView that is bound to a collection of custom objects?

查看:552
本文介绍了如何排序绑定到自定义对象的集合中的DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直跟着本指南在Windows数据绑定窗体控件(MAD道具笔者,本指南是伟大的),我已经用它来创建一个自定义类,并绑定一个 DataGridView的来的集合这个类:

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; } }
}



在这一点上,我输入一组数据从不同的SQL数据库来填充表,和原来大。该指南,甚至提供了添加过滤器,我打算跟随其后有点一个很好的起点。现在,虽然,我坚持我的新生成的 DataGridView的排序。环顾四周,我发现了 DataGridView的都有自己的分类方法,可以使用这样的:

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);



然而,当我尝试这样做,我得到一个出现InvalidOperationException ,告诉我:如果它被绑定到不支持排序的IBindingList的DataGridView控件不能进行排序。我发现无论是IBindingList的和IBindingListView接口,但让我的类继承的任一接口并不能解决这个问题。

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...

推荐答案

有大量的这些可以从谷歌,例如通过搜索C#排序结合名单。 T> 和实施 SupportsSortingCore

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.

这篇关于如何排序绑定到自定义对象的集合中的DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆