C#、BindingList、BindingSource、对 ListBox 进行排序?

发布于 2024-11-05 03:42:51 字数 401 浏览 4 评论 0原文

我已经用谷歌搜索了一个小时,但没有找到有效的答案。

我有一个包含不同成员的 myClass,例如字符串名称和一些 int 数字。然后我有一个列表类型的 myList,然后我有一个列表框控件。

我可以使用 DisplayMember = "Name"DataSource = new BindingList(myList.Values) 轻松地在列表框中显示列表。

但我想要的是让我班上的不同成员可以对其进行排序。我已经尝试创建列表的 BindingSource 并将其传递到列表框,但列表框中的唯一条目是“(枚举)”(绑定列表的类型,但不是其成员)。

我需要什么才能在简单的列表框控件中显示简单的列表并使其可按 T 的成员排序?

I already googled for an hour now and didn't find a working answer.

I have a myClass with different members such as a string Name and some int Numbers. Then I have a myList of type List and then I have a Listbox control.

I can easily display the List in the Listbox by using DisplayMember = "Name" and DataSource = new BindingList<myClass>(myList.Values).

But what I want is to make it sortable by the different Members of my class. I already tried creating a BindingSource of my List and pass it to the Listbox, but then the only entry in my Listbox is "(Enumeration)" (the type of the bindinglist, but not its members).

What do I need to display a simple List in a simple Listbox control AND make it sortable by the members of T?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-11-12 03:42:51

BindingList 本身并不实现排序,尽管它为您提供了这种功能(如果您选择实现它)。 您可以继承 BindingList 并从这里开始。请记住,排序操作可能不应修改原始列表。

有一些现有的实现,例如 BindingListView 也可以满足您的需求。

但是,我发现的最简单的方法是使用填充了类成员的 DataTable。这将让您免费利用 DataView,它已经支持排序、过滤和所有好东西。

BindingList doesn't implement sorting by itself, although it offers you that capability should you choose to implement it. You can inherit from BindingList and start from here. Keep in mind that sort operations probably shouldn't modify the original list.

There are existing implementations, like the BindingListView that will also give you what you need.

However, the easiest way I've found is to use a DataTable populated with the members of the class. This will let you leverage the DataView for free which already supports sorting, filtering, and all the nice things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文