如何排序数据绑定的DataGridView列? [英] How to sort databound DataGridView column?
问题描述
我知道有很多关于这个主题的问题。我已经通过所有的人,但似乎没有任何帮助。
如何通过单击列标题进行排序?
我应该如何修改此code做这项工作?
公共部分Form1类:表格
{ 公共Form1中()
{ 清单< MyClass的>名单=新名单,LT; MyClass的>();
list.Add(新MyClass的(彼得,1202));
list.Add(新MyClass的(詹姆斯,292));
list.Add(新MyClass的(债券,23)); BindingSource的BS =新的BindingSource();
bs.DataSource =清单; DataGridView的DG =新的DataGridView(); DataGridViewTextBoxColumn C =新DataGridViewTextBoxColumn();
c.Name =名;
c.DataPropertyName =姓名;
dg.Columns.Add(C); C =新DataGridViewTextBoxColumn();
c.Name =数字;
c.DataPropertyName =号码;
dg.Columns.Add(C); dg.DataSource = BS; this.Controls.Add((控制)DG); }}MyClass类:IComparable的< MyClass的>
{
公共字符串名称{;组; }
公众诠释号码{搞定;组; } 公共MyClass的(){} 公共MyClass的(字符串名称,诠释号)
{
名称=名称;
数=数;
} 公共重写字符串的ToString()
{
返回的String.Format({0}:{1},姓名,号码);
} #地区IComparable的< MyClass的>会员 公众诠释的CompareTo(MyClass的除外)
{
返回Name.CompareTo(other.Name);
} #endregion
}
我记得有找到的东西,当我整理添加到我的DataGrid中也将工作的问题。您可以通过先添加下面的类到项目实施排序可绑定列表。这是一个列表实现实现的BindingList< T>
,让您可以将DataGrid绑定到它,它也支持排序。细节更好的解释比我可以给是 MSDN这里
公共类SortableBindingList< T> :&的BindingList LT; T>
{
私人ArrayList的排序列表;
私人ArrayList的unsortedItems;
私人布尔isSortedValue;公共SortableBindingList()
{
}公共SortableBindingList(IList的< T>清单)
{
的foreach(列表中的对象o)
{
this.Add((T)O);
}
}保护覆盖布尔SupportsSearchingCore
{
得到
{
返回true;
}
}保护覆盖INT FindCore(PropertyDescriptor的道具,对象键)
{
的PropertyInfo propInfo = typeof运算(T).GetProperty(prop.Name);
Ť项目; 如果(关键!= NULL)
{
的for(int i = 0; I<计数; ++ I)
{
项目=(T)项目[I];
如果(propInfo.GetValue(项目,空).Equals(密钥))
返回我;
}
}
返回-1;
}公众诠释查找(字符串属性,对象键)
{
PropertyDescriptorCollection属性=
TypeDescriptor.GetProperties(typeof运算(T));
PropertyDescriptor的道具= properties.Find(房地产,真实); 如果(道具== NULL)
返回-1;
其他
返回FindCore(道具,键);
}保护覆盖布尔SupportsSortingCore
{
获得{返回true; }
}
保护覆盖布尔IsSortedCore
{
{返回isSortedValue; }
}ListSortDirection sortDirectionValue;
PropertyDescriptor与sortPropertyValue;保护覆盖无效ApplySortCore(PropertyDescriptor的道具,
ListSortDirection方向)
{
排序列表=新的ArrayList(); 键入interfaceType = prop.PropertyType.GetInterface(IComparable的); 如果(interfaceType == NULL和放大器;&安培; prop.PropertyType.IsValueType)
{
键入underlyingType = Nullable.GetUnderlyingType(prop.PropertyType); 如果(underlyingType!= NULL)
{
interfaceType = underlyingType.GetInterface(IComparable的);
}
} 如果(interfaceType!= NULL)
{
sortPropertyValue =道具;
sortDirectionValue =方向; IEnumerable的< T>查询= base.Items;
如果(方向== ListSortDirection.Ascending)
{
查询= query.OrderBy(ⅰ= GT; prop.GetValue(ⅰ));
}
其他
{
查询= query.OrderByDescending(ⅰ= GT; prop.GetValue(ⅰ));
}
INT newIndex = 0;
的foreach(在查询对象的项目)
{
this.Items [newIndex] =(T)项目;
newIndex ++;
}
isSortedValue =真;
this.OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1)); }
其他
{
抛出新NotSupportedException异常(无法通过排序+ prop.Name +
这+ prop.PropertyType.ToString()+
不实现IComparable);
}
}保护覆盖无效RemoveSortCore()
{
INT位置;
对象温度; 如果(unsortedItems!= NULL)
{
的for(int i = 0; I< unsortedItems.Count;)
{
位置= this.Find(姓氏
unsortedItems [I] .GetType()。
的getProperty(姓氏)的GetValue(unsortedItems [I],NULL))。
如果(位置大于0&放大器;&放大器;!位置=ⅰ)
{
TEMP =这个[我]
这[一] =本[位置]
这个[位置] =(T)温度;
我++;
}
否则,如果(位置== I)
我++;
其他
unsortedItems.RemoveAt(ⅰ);
}
isSortedValue = FALSE;
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
}公共无效RemoveSort()
{
RemoveSortCore();
}
保护覆盖的PropertyDescriptor SortPropertyCore
{
{返回sortPropertyValue; }
}保护覆盖ListSortDirection SortDirectionCore
{
{返回sortDirectionValue; }
}}
通过在地方,只有你需要对你在上面张贴的code中的变化是根据你的清单上创建一个SortableBindingList并绑定到排序列表,而不是标准之一,像这样
列表< MyClass的>名单=新名单,LT; MyClass的>();
list.Add(新MyClass的(彼得,1202));
list.Add(新MyClass的(詹姆斯,292));
list.Add(新MyClass的(债券,23));//添加排序列表...
SortableBindingList< MyClass的> sortableList =新SortableBindingList< MyClass的>(名单);BindingSource的BS =新的BindingSource();
bs.DataSource = sortableList; //绑定到排序列表
而这将足以让你去。
I know that there are a lot of questions on this topic. I have been through all of them but nothing seems to help.
How to sort by clicking on column header?
How should I modify this code to do the job?
public partial class Form1 : Form
{
public Form1()
{
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass("Peter", 1202));
list.Add(new MyClass("James", 292));
list.Add(new MyClass("Bond", 23));
BindingSource bs = new BindingSource();
bs.DataSource = list;
DataGridView dg = new DataGridView();
DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
c.Name = "name";
c.DataPropertyName = "Name";
dg.Columns.Add(c);
c = new DataGridViewTextBoxColumn();
c.Name = "number";
c.DataPropertyName = "Number";
dg.Columns.Add(c);
dg.DataSource = bs;
this.Controls.Add((Control)dg);
}
}
class MyClass:IComparable<MyClass>
{
public string Name { get; set; }
public int Number { get; set; }
public MyClass(){}
public MyClass(string name,int number)
{
Name = name;
Number = number;
}
public override string ToString()
{
return string.Format("{0}:{1}",Name,Number);
}
#region IComparable<MyClass> Members
public int CompareTo(MyClass other)
{
return Name.CompareTo(other.Name);
}
#endregion
}
I recall having issues finding something that would work when I added sorting to my datagrids too. You can implement a sortable bindable list by first adding the following class to your project. It is a list implementation that implements BindingList<T>
, so that you can bind your datagrid to it, and it also supports sorting. A better explanation of the details than I could give is on MSDN here
public class SortableBindingList<T> : BindingList<T>
{
private ArrayList sortedList;
private ArrayList unsortedItems;
private bool isSortedValue;
public SortableBindingList()
{
}
public SortableBindingList(IList<T> list)
{
foreach (object o in list)
{
this.Add((T)o);
}
}
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
PropertyInfo propInfo = typeof(T).GetProperty(prop.Name);
T item;
if (key != null)
{
for (int i = 0; i < Count; ++i)
{
item = (T)Items[i];
if (propInfo.GetValue(item, null).Equals(key))
return i;
}
}
return -1;
}
public int Find(string property, object key)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
PropertyDescriptor prop = properties.Find(property, true);
if (prop == null)
return -1;
else
return FindCore(prop, key);
}
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override bool IsSortedCore
{
get { return isSortedValue; }
}
ListSortDirection sortDirectionValue;
PropertyDescriptor sortPropertyValue;
protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortedList = new ArrayList();
Type interfaceType = prop.PropertyType.GetInterface("IComparable");
if (interfaceType == null && prop.PropertyType.IsValueType)
{
Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
if (underlyingType != null)
{
interfaceType = underlyingType.GetInterface("IComparable");
}
}
if (interfaceType != null)
{
sortPropertyValue = prop;
sortDirectionValue = direction;
IEnumerable<T> query = base.Items;
if (direction == ListSortDirection.Ascending)
{
query = query.OrderBy(i => prop.GetValue(i));
}
else
{
query = query.OrderByDescending(i => prop.GetValue(i));
}
int newIndex = 0;
foreach (object item in query)
{
this.Items[newIndex] = (T)item;
newIndex++;
}
isSortedValue = true;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
else
{
throw new NotSupportedException("Cannot sort by " + prop.Name +
". This" + prop.PropertyType.ToString() +
" does not implement IComparable");
}
}
protected override void RemoveSortCore()
{
int position;
object temp;
if (unsortedItems != null)
{
for (int i = 0; i < unsortedItems.Count; )
{
position = this.Find("LastName",
unsortedItems[i].GetType().
GetProperty("LastName").GetValue(unsortedItems[i], null));
if (position > 0 && position != i)
{
temp = this[i];
this[i] = this[position];
this[position] = (T)temp;
i++;
}
else if (position == i)
i++;
else
unsortedItems.RemoveAt(i);
}
isSortedValue = false;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
}
public void RemoveSort()
{
RemoveSortCore();
}
protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyValue; }
}
protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionValue; }
}
}
With that in place, the only changes you need to make to the code that you have posted above is to create a SortableBindingList based on your list and bind to the sortable list, rather than the standard one, like so:
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass("Peter", 1202));
list.Add(new MyClass("James", 292));
list.Add(new MyClass("Bond", 23));
// Added sortable list...
SortableBindingList<MyClass> sortableList = new SortableBindingList<MyClass>(list);
BindingSource bs = new BindingSource();
bs.DataSource = sortableList; // Bind to the sortable list
And that will be enough to get you going.
这篇关于如何排序数据绑定的DataGridView列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!