C#、BindingList、BindingSource、对 ListBox 进行排序?
我已经用谷歌搜索了一个小时,但没有找到有效的答案。
我有一个包含不同成员的 myClass,例如字符串名称和一些 int 数字。然后我有一个列表类型的 myList,然后我有一个列表框控件。
我可以使用 DisplayMember = "Name"
和 DataSource = new BindingList
轻松地在列表框中显示列表。
但我想要的是让我班上的不同成员可以对其进行排序。我已经尝试创建列表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.