Ian*_*ien 3 .net c# datagridview
我相信这应该自动处理.我绑定DataGridView到一个对象数组:
public class Entity {
public string Name { get; set; }
public int PrimaryKey { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
绑定网格:
public void BindGrid(Entity[] entities) {
grdEntities.DataSource = entities;
}
Run Code Online (Sandbox Code Playgroud)
当我单击"名称"列中的列标题时,即使将SortMode其设置为"自动",也不会发生任何事情.列标题中也缺少排序字形.
我试过绑定到一个IBindingList也是一个,IList但这没有用.
我希望有一个简单,优雅的解决方案,可以在DataGridView或者设置属性,DataGridViewColumn而不是必须创建一个新类来支持排序.通过单击DataBound上的标题,我应该怎么做才能支持对列进行排序DataGridView?
我创建了一个新的基于IComparer的界面,允许您指定列和方向.我只是这样做是因为我需要我的排序代码尽可能通用 - 我有两个网格需要像这样排序,我不想保持两倍的代码.这是界面,非常简单:
public interface IByColumnComparer : IComparer
{
string SortColumn { get; set; }
bool SortDescending { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
显然,如果你不担心保持通用(你可能应该),那么这并不是绝对必要的.然后,我构建了一个基于BindingList <>的新类.这允许我覆盖排序代码并逐列提供我自己的IByColumnComparer,这是我所需要的灵活性.看一下这个:
public class SortableGenericCollection<T> : BindingList<T>
{
IByColumnComparer GenericComparer = null;
public SortableGenericCollection(IByColumnComparer SortingComparer)
{
GenericComparer = SortingComparer;
}
protected override bool SupportsSortingCore
{
get
{
return true;
}
}
protected override bool IsSortedCore
{
get
{
for (int i = 0; i < Items.Count - 1; ++i)
{
T lhs = Items[i];
T rhs = Items[i + 1];
PropertyDescriptor property = SortPropertyCore;
if (property != null)
{
object lhsValue = lhs == null ? null :
property.GetValue(lhs);
object rhsValue = rhs == null ? null :
property.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = GenericComparer.Compare(lhs, rhs);
}
if (result >= 0)
{
return false;
}
}
}
return true;
}
}
private ListSortDirection sortDirection;
protected override ListSortDirection SortDirectionCore
{
get
{
return sortDirection;
}
}
private PropertyDescriptor sortProperty;
protected override PropertyDescriptor SortPropertyCore
{
get
{
return sortProperty;
}
}
protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortProperty = prop;
sortDirection = direction;
GenericComparer.SortColumn = prop.Name;
GenericComparer.SortDescending = direction == ListSortDirection.Descending ? true : false;
List<T> list = (List<T>)Items;
list.Sort(delegate(T lhs, T rhs)
{
if (sortProperty != null)
{
object lhsValue = lhs == null ? null :
sortProperty.GetValue(lhs);
object rhsValue = rhs == null ? null :
sortProperty.GetValue(rhs);
int result;
if (lhsValue == null)
{
result = -1;
}
else if (rhsValue == null)
{
result = 1;
}
else
{
result = GenericComparer.Compare(lhs, rhs);
}
return result;
}
else
{
return 0;
}
});
}
protected override void RemoveSortCore()
{
sortDirection = ListSortDirection.Ascending;
sortProperty = null;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑这应该提供一些有关如何基于我的界面创建自己的IComparer的信息. 基于接口拥有自己的IComparer的优点是,您可以单向排序某些列,而另一列排序(某些列可能是字符串,有些是int,有些列可能有关于最重要的内容的特殊规则等).以下是IComparer如何工作的示例:
public class MyGenericComparer : IByColumnComparer
{
private string columnToCompare;
private bool descending;
public string SortColumn
{
get { return columnToCompare; }
set { columnToCompare = value; }
}
public bool SortDescending
{
get { return descending; }
set { descending = value; }
}
public MyGenericComparer(string column, bool descend)
{
columnToCompare = column;
descending = descend;
}
public int Compare(object x, object y)
{
MyGenericObject firstObj = (MyGenericObject )x;
MyGenericObject secondObj = (MyGenericObject )y;
if (descending)
{
MyGenericObject tmp = secondObj ;
secondObj = firstObj ;
firstObj = tmp;
}
if (columnToCompare == "StringColumn")
{
//Run code to compare strings, return the appropriate int
//eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
}
if (columnToCompare == "IntColumn")
{
//Run code to compare ints, return the appropriate int
//eg, "1" if firstObj was greater, "-1" is secondObj, "0" if equal
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您所要做的就是使用比较器的实例创建列表!
public static MyGenericComparer GridComparer = new MyGenericComparer();
public static SortableGenericCollection<GenericObject> GridList = new SortableGenericCollection<GenericObject>(GridComparer);
Run Code Online (Sandbox Code Playgroud)