的WinForms:DataGridView的 - 纲领性排序 [英] WinForms: DataGridView - programmatic sorting
问题描述
我有与它datagridview的一种形式。的dataGridView被绑定到BindingSource的:
I've got a form with datagridview on it. dataGridView is binded to BindingSource:
public class Address
{
public string State { get; set; }
public string City { get; set; }
public string Street { get; set; }
}
this.addressBindingSource.DataSource = typeof(Address);
this.dataGridView1.DataSource = this.addressBindingSource;
我填写数据源
是这样的:
addressBindingSource.DataSource = new BindingList<Address>
{
new Address {State = "S1", City = "C1", Street = "S1"},
new Address {State = "S1", City = "C1", Street = "S2"},
new Address {State = "S1", City = "C1", Street = "S3"},
new Address {State = "S1", City = "C2", Street = "S4"},
new Address {State = "S1", City = "C2", Street = "S5"},
new Address {State = "S1", City = "C2", Street = "S6"},
};
我试图使分拣此datagridview的。我设置 SortMode
到编程
为dataGridView1的所有列。我加的事件处理程序 ColumnHeaderMouseClick
:
I'm trying to enable sorting for this datagridview. I set SortMode
to Programmatic
for all the columns of dataGridView1. And I added an event handler for ColumnHeaderMouseClick
:
private Dictionary<int, string> columnIndexPropertyNameDictionary = new Dictionary<int, string>
{
{0, "State"},
{1, "City"},
{2, "Street"},
};
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex < 0)
return;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (i == e.ColumnIndex)
continue;
dataGridView1.Columns[i].HeaderCell.SortGlyphDirection = SortOrder.None;
}
var column = dataGridView1.Columns[e.ColumnIndex];
if (column.SortMode != DataGridViewColumnSortMode.Programmatic)
return;
var sortGlyphDirection = column.HeaderCell.SortGlyphDirection;
switch (sortGlyphDirection)
{
case SortOrder.None:
case SortOrder.Ascending:
addressBindingSource.Sort = columnIndexPropertyNameDictionary[e.ColumnIndex] + " ASC";
column.HeaderCell.SortGlyphDirection = SortOrder.Descending;
break;
case SortOrder.Descending:
addressBindingSource.Sort = columnIndexPropertyNameDictionary[e.ColumnIndex] + " DESC";
column.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
break;
}
}
排序仍然无法正常工作。我在做什么错了?
Sorting still doesn't work. What am I doing wrong?
推荐答案
的问题是,开箱的的BindingList不支持排序!我知道 - 听起来很愚蠢的,但就是它是如何
The problem is that out of the box the BindingList does not support sorting! I know - sounds dumb but that is how it is.
您需要实现自己的SortableBindingList。对于该代码的一个例子是以下
You need to implement your own SortableBindingList. An example of the code for that is below.
此代码来自这里,我没有时间彻底检查一下。如果不工作,然后谷歌术语SortableBindingList,也有实现的地段在那里。
This code came from here and I don't have time to check it thoroughly. If it doesn't work then google the term SortableBindingList, there are lots of implementations out there.
public class SortableBindingList<t> : BindingList<t>
{
private bool m_Sorted = false;
private ListSortDirection m_SortDirection = ListSortDirection.Ascending;
private PropertyDescriptor m_SortProperty = null;
protected override bool SupportsSortingCore
{
get
{
return true;
}
}
protected override bool IsSortedCore
{
get
{
return m_Sorted;
}
}
protected override ListSortDirection SortDirectionCore
{
get
{
return m_SortDirection;
}
}
protected override PropertyDescriptor SortPropertyCore
{
get
{
return m_SortProperty;
}
}
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
m_SortDirection = direction;
m_SortProperty = prop;
var listRef = this.Items as List<t>;
if (listRef == null)
return;
var comparer = new SortComparer<t>(prop, direction);
listRef.Sort(comparer);
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
这篇关于的WinForms:DataGridView的 - 纲领性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!