如:
我用select * from myclass 绑定到DataGrid后
我点击按钮B1想对DataGrid的年龄进行降序排列注:这里不再访问数据库,而是要直接对DataGrid进行排序
    谢谢

解决方案 »

  1.   

    在DataGrid的属性生成器里面列填写排序表达式,可见。2003版本
      

  2.   

    SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM " +
             "Authors", myConnection);
          // Fill a new DataSet.
          DataSet ds = new DataSet();
          myCommand.Fill(ds);
          // Create a DataView based on the first (and only) DataTable in the
          // DataSet. Sort the data using the specified sort field.
          DataView view = ds.Tables[0].DefaultView;
          // To sort in ascending order:
          view.Sort = sortfield + " ASC";
          // To sort in descending order:
          // view.Sort = sortfield + " DESC";
          // Bind the DataGrid to the sorted DataView.
          MyDataGrid.DataSource=view;
          MyDataGrid.DataBind();
      

  3.   

    上面的排序我知道
    但我这里要求是:
    这里不再访问数据库,而是要直接对DataGrid进行排序
        谢谢
      

  4.   

    在DataGrid的属性生成器里面列填写排序表达式
      

  5.   

    第一次查询后 用ViewState 把 ds 保存起来
    排序的时候在用上面的 对 ds 排序 重新绑顶下
      

  6.   

    那就对DataView进行排序,然后把DataView绑定到DataGrid
      

  7.   

    DataView dv = dt_table.DefaultView;
    dv.Sort = "你要排序的字段";
    重新绑定到DataGrid
      

  8.   

    DataGrid的排序就是后台数据源的排序,其他随意
      

  9.   

    就没有办法直接让DataGrid排序的方法吗
      

  10.   

    [转载]DataGrid实现排序功能
                                           DataGrid是ASP.NET中非常重要的一个控件。它能方便的让我们实现编辑、排序功能;但是排序功能默认的是升序(ASC),能不能让DataGrid同时实现升降序排列呢?这篇文章将给你一个比较好的解决方法。下面的例子将告诉你如何给DataGrid动态添加sortexpression 和 sortdirection 属性,并通过DataView使DataGird中的数据按照这两个属性排列。在这个例子中使用的DataGrid的sortexpression属性只需要在sortcommand事件中设置(和通常的排序一样),DataGrid的sortexpression 属性保存了最后一次用来排序的字段名称,DataGrid的sortdirection 属性保存了最后一次用来排序的字段排列方式(“ASC”或者“DESC”) 页面设计:1.       在页面上添加一个DataGrid;2.       设置DataGrid的AllowSorting属性为True;3.       设置AutogenerateColumns 属性为False;4.       添加要绑定的字段到DataGrid,并按照下表设置其属性。Sortexpression属性和数据库中数据表中的字段名保持一致。我使用Northwind数据库中的Employees来说明这个例子。DataField (字段名)         Header Text(显示的名称,解释)           Sort Expression(字段名,排序用)
     记得给datagrid绑定字段时候,设置上面三种属性
    EmployeeID
     Employee ID
     EmployeeID
     
    LastName
     Last Name
     LastName
     
    FirstName
     First Name
     FirstName
     
    Title
     Title
     Title
     
    City
     City
     City
     
    Country
     Country
     Country程序代码设计:1.       在页面第一次被加载时给DATAGRID添加动态属性sortexpression 和 sortdirection;2.       给指定的字段的sortexpression 和 sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式;3.       得到dataset 和 dataview 对象。设置dataview 的sort 属性为动态属性sortexpression 和 sortdirection的连接字符串;4.       用dataview绑定datagrid;5.       当表头被点击时,触发datagrid的sortcommand事件;6.       得到sortcommand事件传递过来的sortexpression属性,并与datagrid的sortexpression属性比较;7.       如果找到相等的a)       先检查sortdirection属性;b)       If sortdirection属性等于“ASC”then                                    i.        设置sortdirection属性的值等于“DESC”c)        Else                                    i.        设置sortdirection属性的值等于“ASC”d)       End If8.       重复第3步注意:当你运行下面的代码并点击同一列两次或者两次以上,此列的排列方式会自动根据现有的排列方式的反序排列。下面的下面的代码将给你演示怎么使用DataGrid的动态属性和DataView对象。C# Code:private void Page_Load(object sender, System.EventArgs e){    // 在此处放置用户代码以初始化页面    if(!Page.IsPostBack)    {        if(DataGrid1.Attributes["SortExpression"] == null)        {            DataGrid1.Attributes["SortExpression"] = "LastName";            DataGrid1.Attributes["SortDirection"] = "ASC";        }        BindGrid();    }} private void BindGrid(){    SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");    conn.Open();    SqlDataAdapter cmd = new SqlDataAdapter("select * from Employees",conn);    DataSet ds = new DataSet();    cmd.Fill(ds,"Employees");    DataView dv = new DataView();    dv = ds.Tables[0].DefaultView;    string SortExpression = DataGrid1.Attributes["SortExpression"];    string SortDirection = DataGrid1.Attributes["SortDirection"];    dv.Sort = SortExpression + " " + SortDirection;     DataGrid1.DataSource = dv;    DataGrid1.DataBind();} private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e){    string SortExpression = e.SortExpression.ToString();    string SortDirection = "ASC";    if(SortExpression == DataGrid1.Attributes["SortExpression"])    {        SortDirection = (DataGrid1.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");    }    DataGrid1.Attributes["SortExpression"] = SortExpression;    DataGrid1.Attributes["SortDirection"] = SortDirection;    BindGrid();}