绑定到DataGridView的自定义类:如何排序? [英] Custom class bound to a DataGridView: how to sort?
问题描述
我搜索了CP,但找不到有用的东西.这是我的问题:
我有一个自定义类NetworkGroup
,它表示Active Directory中安全组上的数据.没什么,只是显示名称,描述,路径和extensionattribute10
中存储的值.第二个类NetworkGroupList
从List(Of NetworkGroup)
继承.
我正在使用NetworkGroupList
的实例作为只读DataGridView
的数据源,并且工作正常,但有一个烦恼:单击列标题不会对数据进行排序.默认情况下,列上的SortMode
属性设置为Automatic
;默认情况下,属性设置为Automatic
.我到目前为止已经以编程方式对其进行了设置.没什么区别:我的班级缺少一些功能,该功能允许网格按列自动排序.绑定到DataTable
对象的类似网格可以很好地排序,并且我已经考虑过手动将列表复制到表中并使用它,这似乎有些过头.我已经修过了BindableList(Of T)
类,但没有运气:文档非常零散,对于所涉及的工作,似乎建立数据表会容易得多.
I searched CP, and could not find anything that was useful. Here is my issue:
I have a custom class, NetworkGroup
, representing data on security groups from Active Directory. Nothing fancy, just the display name, description, path and a value stored in extensionattribute10
. A second class, NetworkGroupList
, inherits from List(Of NetworkGroup)
.
I am using an instance of NetworkGroupList
as the data source for a read-only DataGridView
and it works fine, with one annoyance: clicking on the column headers does not sort the data. The SortMode
property on the columns are set to Automatic
by default; I have gone so far as to set them programmatically. Makes no difference: my class lacks some functionality that allows the grid to auto-sort by column. Similar grids bound to DataTable
objects sort just fine, and I have considered manually copying the list into a table and using that, which seems overkill. I have tinkered with the BindableList(Of T)
class, with no luck: documentation is very spotty, and for the work involved, it seems that building a data table would be much easier.
Any suggestions on how to proceed?
推荐答案
我看了一些使用DataGridView并进行很好排序的旧项目.
他们都使用DataTable
数据表dt;
数据集ds;
DataGridView mDgv;
dt = ds.Tables [ixTbl];
mDgv.DataSource = dt;
I looked at some of my old projects that use DataGridView and sort nicely.
They all use DataTable
DataTable dt;
DataSet ds;
DataGridView mDgv;
dt=ds.Tables[ixTbl];
mDgv.DataSource =dt;
DataTable dt;
DataSet ds;
DataGridView mDgv;
dt=ds.Tables[ixTbl];
mDgv.DataSource =dt;
因此,可以建立该DataTable.
So yes build that DataTable.
这篇关于绑定到DataGridView的自定义类:如何排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!