Custom sorting in the DataGridView
DataGridView对此一无所知
当然,排名。默认值
排序方案(自动排序
对于数据绑定列)调用
通过列的DataGridView.Sort()。
头鼠标单击只是委托给
IBindingList实现
applysort()在您绑定的列表中
到网格。通常,这个列表
将是一个数据视图。用我的
ObjectListView实现,此
将是任意列表的视图
业务对象。不管怎样,你结束了
向上比较
列表中的项使用
可比较的
属性类型。
细节
PropertyComparer属性公开
属性比较器集合,其中
是属性名称键的字典
和iComparer值。你可以代替
属性的IComparer,方法是使用
PropertyComparersCollection.Add()。
方法或索引器(例如
视图.propertycomparer[“propname”]=
MySimple)。恢复为默认值
IComparable排序,使用
属性比较器集合。移除()
方法或将IComparer值设置为
通过索引器为空。
如果将IComparer添加到
属性比较器集合,它将
用于所有后续排序,直到
它从集合中移除,或者
它被另一个比较程序替换。
如果ObjectListView已经
将IComparer添加到时排序
或从属性比较器中删除,
视图将自动重新排序。
如果要更改多个
视图后的属性比较器为
排序后,可以使用ObjectListView
BeginUpdate()和EndUpdate()方法
抑制ListChanged和Sort
事件,直到所有IComparers
已更改。这防止
多次刷新
DATAGIDVIEW。如果对象列表视图
在IComparers时未排序
添加或删除,无自动
重新排序完成。
注意,当对多个
列,自定义IComparer可以
与一个排序属性一起使用,并且
默认的IComparable排序。
例如:
private void radioButtonSortProgram_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButtonSortProgramAlpha.Checked)
this.view.PropertyComparers["Program"] = new CustomerProgramComparerAlpha();
else if (this.radioButtonSortProgramRank.Checked)
this.view.PropertyComparers["Program"] = new CustomerProgramComparerRank();
else
this.view.PropertyComparers.Remove("Program");
}