我绑定数据源的开始用 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);
        }
---显示数据的就不多写了,怎么去写排序呢?
  }
怎么让去排序,在线等待的啊。急的很。

解决方案 »

  1.   

    using System;
    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);
                }
            }       
        }
    }
      

  2.   

    你没有理解意思,那是DataGridview啊,点击每一列的头,
      

  3.   

    我试了才说的,用ArrayList 绑定数据点击表头不排序的。
      

  4.   


    确实啊  想不到什么好办法  把ArrayList转换成table吧
      

  5.   

    public class SortableBindingList<T> : BindingList<T>
        {
            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());
            }
        } 
      

  6.   

    我的不是这个啊 ,
    public class ArrayListToConvert : ArrayList, IBindingList

    }
      

  7.   

    你的这个我看见了  http://blog.163.com/shensc@126/blog/static/13128965220107101628961/
      我现在不能那样的做的,具体的原因是<T> 我现在做的是控件,反射也不好弄。关于泛型的先放下, 泛型能实现,我直接继承IBindingList 去排序,理论上说应该可以的。
    可是怎么实现,真的很着急的。
      

  8.   

    主要是实现 IBindingList.ApplySort 方法。
    其实代码和上面贴出来的基本类似。你也可以继承 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;
            }
      

  9.   

    这样可以绑定数组并点击Header后可以排序(设置GridView的AllowSorting="True")ArrayList arr = null;
        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();
        }
      

  10.   

    谢谢你,对你的崇拜是没法用语言来表达的,我到现在崇拜三个人了,第一个曾今讲过委托和事件的张子阳,第二个制作控件的Start ,还加上就是你, 上一次的问题我很满意,技术好的人很多,这样耐心看问题和的热心回答的人不是特别多,更何况你两者都具备, 现在解决我的困扰了,我刚刚毕业,特别是以前搞BS 现在转航了.如果你来我们公司的话 一月少了12000的话,感觉就是埋没你了。