我绑定数据源的开始用 ArrayList Templist = new ArrayList();
DataGridView1.DataSource=Templist ;
显示数据没有问题, 现在遇到的问题就是不可以排序,
现在
public class ArrayListToConvert : ArrayList, IBindingList
{
public override int Add(object value)
{
int index = base.Count;
base.Add(value);
OnInsertComplete(index, value);
return index;
} public override void Insert(int index, object value)
{
base.Insert(index, value);
OnInsertComplete(index, value);
}
---显示数据的就不多写了,怎么去写排序呢?
}
怎么让去排序,在线等待的啊。急的很。
DataGridView1.DataSource=Templist ;
显示数据没有问题, 现在遇到的问题就是不可以排序,
现在
public class ArrayListToConvert : ArrayList, IBindingList
{
public override int Add(object value)
{
int index = base.Count;
base.Add(value);
OnInsertComplete(index, value);
return index;
} public override void Insert(int index, object value)
{
base.Insert(index, value);
OnInsertComplete(index, value);
}
---显示数据的就不多写了,怎么去写排序呢?
}
怎么让去排序,在线等待的啊。急的很。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
arr.Add(2);
arr.Add(22);
arr.Add(1);
arr.Sort();
foreach (var item in arr)
{
Console.WriteLine(item);
}
}
}
}
确实啊 想不到什么好办法 把ArrayList转换成table吧
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem; public SortableBindingList() : base() { } public SortableBindingList(IList<T> list) : base(list) { } protected override bool SupportsSortingCore
{
get { return true; }
} protected override bool SupportsSearchingCore
{
get { return true; }
} protected override bool IsSortedCore
{
get { return isSortedCore; }
} protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionCore; }
} protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyCore; }
} protected override int FindCore(PropertyDescriptor prop, object key)
{
for (int i = 0; i < this.Count; i++)
{
if (Equals(prop.GetValue(this), key)) return i;
}
return -1;
} protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = prop;
sortDirectionCore = direction;
Sort();
} protected override void RemoveSortCore()
{
if (isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
Sort();
}
} public string DefaultSortItem
{
get { return defaultSortItem; }
set
{
if (defaultSortItem != value)
{
defaultSortItem = value;
Sort();
}
}
} private void Sort()
{
List<T> list = (this.Items as List<T>);
list.Sort(CompareCore);
ResetBindings();
} private int CompareCore(T o1, T o2)
{
int ret = 0;
if (SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
if (property != null)
{
ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
}
}
if (SortDirectionCore == ListSortDirection.Descending) ret = -ret;
return ret;
} private static int CompareValue(object o1, object o2, Type type)
{
//这里改成自己定义的比较
if (o1 == null) return o2 == null ? 0 : -1;
else if (o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}
public class ArrayListToConvert : ArrayList, IBindingList
{
}
我现在不能那样的做的,具体的原因是<T> 我现在做的是控件,反射也不好弄。关于泛型的先放下, 泛型能实现,我直接继承IBindingList 去排序,理论上说应该可以的。
可是怎么实现,真的很着急的。
其实代码和上面贴出来的基本类似。你也可以继承 BindingList<object> 这样重写的方法更少。下面的使用:
ArrayList list1 = new ArrayList();
list1.Add(new { ID = 123, Name = "test2" });
list1.Add(new { ID = 111, Name = "test1" });
list1.Add(new { ID = 333, Name = "test3" });var sortableList = new SortabledArrayList(list1);
sortableList.DefaultSortItem = "Name";
dataGridView2.DataSource = sortableList;
public class SortabledArrayList : ArrayList, IBindingList, IComparer
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem;
private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1); public SortabledArrayList()
{ } public SortabledArrayList(ArrayList list) : base(list)
{
} #region IBindingList Members public event ListChangedEventHandler ListChanged; public void RemoveIndex(PropertyDescriptor property)
{
throw new NotImplementedException();
}
public void AddIndex(PropertyDescriptor property)
{
throw new NotImplementedException();
} public object AddNew()
{
throw new NotImplementedException();
} public bool AllowEdit
{
get { return true; }
} public bool AllowNew
{
get { return false; }
} public bool AllowRemove
{
get { return true; }
} public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = property;
sortDirectionCore = direction;
SortCore();
} public int Find(PropertyDescriptor property, object key)
{
for (int i = 0; i < this.Count; i++)
{
if (Equals(property.GetValue(this), key)) return i;
}
return -1;
} public bool IsSorted
{
get { return true; }
} public void RemoveSort()
{
if (isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
SortCore();
}
} public ListSortDirection SortDirection
{
get { return sortDirectionCore; }
} public PropertyDescriptor SortProperty
{
get { return sortPropertyCore; }
} public bool SupportsChangeNotification
{
get { return true; }
} public bool SupportsSearching
{
get { return true; }
} public bool SupportsSorting
{
get { return true; }
} #endregion
public string DefaultSortItem
{
get { return defaultSortItem; }
set
{
if (defaultSortItem != value)
{
defaultSortItem = value;
SortCore();
}
}
} private void SortCore()
{
ArrayList list = this as ArrayList;
list.Sort(this);
ResetArrayList();
} protected virtual void ResetArrayList()
{
if (ListChanged != null)
ListChanged(this, resetEvent);
} private static int CompareValue(object o1, object o2, Type type)
{
//这里改成自己定义的比较
if (o1 == null) return o2 == null ? 0 : -1;
else if (o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
else if (type == typeof(DateTime)) return Convert.ToDateTime(o1).CompareTo(o2);
else return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
} #region IComparer Members public int Compare(object x, object y)
{
int ret = 0;
if (SortProperty != null)
{
ret = CompareValue(SortProperty.GetValue(x), SortProperty.GetValue(y), SortProperty.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
if (x == null || y == null)
{
ret = CompareValue(x, y, typeof(object));
}
else
{
PropertyInfo property = x.GetType().GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
if (property != null)
{
ret = CompareValue(property.GetValue(x, null), property.GetValue(y, null), property.PropertyType);
}
}
}
if (SortDirection == ListSortDirection.Descending) ret = -ret;
return ret;
}
protected void Page_Load(object sender, EventArgs e)
{
arr= new ArrayList();
arr.Add(2);
arr.Add(22);
arr.Add(1);
GridView1.DataSource = arr;
DataBind(); }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
arr.Sort();
GridView1.DataSource = arr;
DataBind();
}