就是点击一列的标头就能升啊降啊排序的那种,如果数据源是dataset的话我大致也了解,就是把视图给排一下序,说白了就是对数据源操作。但我的问题是对象数组数据源,因为要用到三层架构啊,有数据实体对象,所以我就懵了。如果只是对一个字段进行排序还好,只要继承一下IComparable接口,用数据对象sort一下也好办,问题是有好多个字段都要可排序T_T等待高手救援……

解决方案 »

  1.   

    grid排序
    用grid的sorting
    事件值得数据列时候舍得排序字段名
      

  2.   

    是用sorting事件啊,但也需要对数据源进行操作吧?还有,对你这句话不明白,能解释下吗?
      

  3.   

    建议你将数组改为List<实体类>在GridView的Sorting事件中直接对数据源排序(假设你的数据源数组是lst.Sort(比较器)),在重新绑定数据就可以了。关于比较器的使用见下例:
    using System;
    using System.Collections.Generic;public class DinoComparer: IComparer<string>
    {
        public int Compare(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                    // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // lengths of the two strings.
                    //
                    int retval = x.Length.CompareTo(y.Length);                if (retval != 0)
                    {
                        // If the strings are not of equal length,
                        // the longer string is greater.
                        //
                        return retval;
                    }
                    else
                    {
                        // If the strings are of equal length,
                        // sort them with ordinary string comparison.
                        //
                        return x.CompareTo(y);
                    }
                }
            }
        }
    }public class Example
    {
        public static void Main()
        {
            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("Pachycephalosaurus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Mamenchisaurus");
            dinosaurs.Add("Deinonychus");
            Display(dinosaurs);        DinoComparer dc = new DinoComparer();        Console.WriteLine("\nSort with alternate comparer:");
            dinosaurs.Sort(dc);
            Display(dinosaurs);        SearchAndInsert(dinosaurs, "Coelophysis", dc);
            Display(dinosaurs);        SearchAndInsert(dinosaurs, "Oviraptor", dc);
            Display(dinosaurs);        SearchAndInsert(dinosaurs, "Tyrannosaur", dc);
            Display(dinosaurs);        SearchAndInsert(dinosaurs, null, dc);
            Display(dinosaurs);
        }    private static void SearchAndInsert(List<string> list, 
            string insert, DinoComparer dc)
        {
            Console.WriteLine("\nBinarySearch and Insert \"{0}\":", insert);        int index = list.BinarySearch(insert, dc);        if (index < 0)
            {
                list.Insert(~index, insert);
            }
        }    private static void Display(List<string> list)
        {
            Console.WriteLine();
            foreach( string s in list )
            {
                Console.WriteLine(s);
            }
        }
    }
      

  4.   

    你说所的数组,是不是对象数组,如果是的化,可以这样实现,下列是实现名字,如果很多的化,就多加类就好了外部对象来控制排序,需要实现:ICompare<T>
    public class NameCompare :ICompare<Student>
    {
    public int Compare(Student x,Student y)
         {
          return (x.name.CompareTo(y.name));
    }
    }Students.Sort(new NameCompare);
      

  5.   

    感谢楼上的,不过你说的方法我也想过,用数组的sort方法只能对一个字段进行排序,如果我的对象有两个属性需要排序,比如Person类的身份证号和性别两个属性,我既要对身份证号可排序,又要对性别可排序,用sort方法就有点无能为力了
      

  6.   

    谁说无能为力?sort方法重载好多种,只要你会用IComparer接口,怎么排都行
      

  7.   

    抱歉,你可能没明白我说的意思,我想要实现的是——还是拿上面的Person类举例,我想点击GridView显示身份证列的列头,数据按照身份证升序或降序。我点击性别列列头,数据按照性别排序。据我薄见,IComparer接口的比较方法只能比较一种结构,比如对性别排序时,当性别相同时在按照身份证排序,这样是可以实现的,但我一会儿要排性别,一会儿又要排身份证,这样的话能做到吗?貌似除了再实现另外一个IComparer接口外别无它法了吧?恕我愚见。
      

  8.   

    这个想法很不错,想偷懒少写代码,我也觉得如果有10个字段,对10个字段排序,就要写10个类,是有点恼火.......可以用委托来试试,但就需要自己写排序,但是要多写10个方法.......还是有点恼火.......
    期待高手了......
    using System;
    using System.Collections.Generic;
    using System.Text;namespace 排序使用委托示例
    {
        delegate bool Compar(object lhs,object rhs);
        class Program
        {
            static void Main(string[] args)
            {
                Compar getMethod = new Compar(Employees.compareSalary);//注意不仅可以比工资,还能比名字
                Employees[] employees = new Employees[] {
                new Employees("shaka",2000),
                    new Employees("jack",1500),
                    new Employees("Rose",1200),
                    new Employees("Ruby",2400),
                    new Employees("Booal",3000)
                };
                Sort.sort(employees, getMethod);
                for (int i = 0; i < employees.Length; i++)
                {
                    Console.WriteLine(employees[i].ToString());
                }
            }
        }
        class Employees
        {
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }        private decimal salary;
            public decimal Salary
            {
                get { return salary; }
                set { salary = value; }
            }        public Employees(string name, decimal salary)
            {
                this.name = name;
                this.salary = salary;
            }        public static bool compareSalary(object lhs, object rhs)
            {
                Employees elhs = (Employees)lhs;
                Employees erhs = (Employees)rhs;
                return (elhs.Salary > erhs.Salary);
            }
            public static bool compareName(object lhs, object rhs)
            {
                Employees elhs = (Employees)lhs;
                Employees erhs = (Employees)rhs;
                return (elhs.Name.Equals(erhs.Name));
            }
            public override string ToString()
            {
                return string.Format(name + ",{0:c}", salary);
            }
        }
        class Sort
        {
            public static void sort(object[] sortArray, Compar getMethod)
            {
                for (int i = 0; i < sortArray.Length; i++)
                {
                    for (int j = i + 1; j < sortArray.Length; j++)
                    {
                        if (getMethod(sortArray[j], sortArray[i]))
                        {
                            object temp = sortArray[i];
                            sortArray[i] = sortArray[j];
                            sortArray[j] = temp;
                        }
                    }
                }
            }
        }
    }
      

  9.   

    我想起了,用LINQDATASOURCE,省事省到家
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                onselecting="LinqDataSource1_Selecting"  >        
            </asp:LinqDataSource>
        
        </div>
        <asp:GridView ID="GridView1" runat="server"
            DataSourceID="LinqDataSource1" AllowSorting="True">
        </asp:GridView>
        </form>
    </body>
    </html>using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;public partial class _Default : System.Web.UI.Page
    {
        Stutent[] students;
        protected void Page_Load(object sender, EventArgs e)
        {
            students = new Stutent[]{
                new Stutent("shaka",22,"男"),
                new Stutent("shaka",12,"男"),
                new Stutent("romeo",34,"男"),
                new Stutent("rose",45,"女"),
                new Stutent("ruby",67,"女"),
                new Stutent("kevin",23,"男"),
                new Stutent("jordan",22,"男")
            };
        }
        protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = students;
        }
    }public class Stutent
    {
        public Stutent(string name, int age, string sex)
        {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        private int age;    public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string sex;    public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        private string name;    public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }