设置DataGrid的Attribute["SortExpression"]为某一列DESC即可,good luck!

解决方案 »

  1.   

    在给datagrid绑定数据时使用的sql语句中先降排序
    select * from YourTableName order by YourColumnName DESC
    OK!!!
      

  2.   

    DataGrid.DefaultView.Sort = "列名 DESC";
      

  3.   

    排序的问题大部分书上都有讲述。在DataGrid标签中加上属性:AllowSorting="True",OnSortCommand="SortGrid"。在后台代码中方法实现:
    void SortGrid( object src ,DataGridSortCommandEventArgs e )
    {
        string strSortFiled = (string) e.SortExpression; //取得排序字段
        string strSQL = "select * from mytable order by " + strSortFiled + " desc"
        //此处省略,作用是重新绑定DataGrid
    }
      

  4.   

    用SQL语句可以了。或者设置DataGrid的Attribute["SortExpression"]为某一列DESC
      

  5.   

    protected  void DataGridMe_Sort(Object sender, DataGridSortCommandEventArgs e) 
    {
    ds =GetDataSet();
    DataView Source = ds.Tables[0].DefaultView;
    Source.Sort =e.SortExpression + " desc";
    DataGridMe.DataSource=Source;
    DataGridMe.DataBind();
    }
      

  6.   

    给你个例子:
    webform.aspx
    <%@ Register TagPrefix="uc1" TagName="UC_Top" Src="../UserControl/UC_Top.ascx" %>
    <%@ Page language="c#" Codebehind="ManageTypeZ.aspx.cs" AutoEventWireup="false" Inherits="Baili.ManageTypeZ" %>
    <%@ Register TagPrefix="cc1" Namespace="Ghost.Pagination" Assembly="Ghost.Pagination" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>ManageTypeZ</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <link rel="stylesheet" type="text/css" href="../Common/Style.css">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="1">
    <TR>
    <TD>
    <asp:Label id="lb_Manage" runat="server"></asp:Label></TD>
    </TR>
    <TR>
    <TD>
    <asp:DataGrid id="dg_TypeZ" runat="server" Width="100%" AutoGenerateColumns="False" AllowSorting="True">
    <Columns>
    <asp:TemplateColumn HeaderText="选择">
    <ItemTemplate>
    <%# ShowCheckBox(int.Parse(DataBinder.Eval(Container.DataItem, "TypeID").ToString())) %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="TypeZ" HeaderText="Z轴类别(中文)">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "TypeZ") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="TypeZEn" HeaderText="Z轴类别(英文)">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "TypeZEn") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="TypeXID" HeaderText="X轴类别">
    <ItemTemplate>
    制鞋工厂
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="TypeYID" HeaderText="Y轴类别">
    <ItemTemplate>
    <%# ShowTypeXYName(int.Parse(DataBinder.Eval(Container.DataItem, "TypeYID").ToString())) %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid></TD>
    </TR>
    <TR>
    <TD align="right">
    <cc1:Pagination id="Paging" runat="server"></cc1:Pagination></TD>
    </TR>
    </TABLE>
    </form>
    </body>
    </HTML>
      

  7.   

    cs文件
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace Baili
    {
    /// <summary>
    /// ManageTypeZ 的摘要说明。
    /// </summary>
    public class ManageTypeZ : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label lb_Manage;
    protected System.Web.UI.WebControls.DataGrid dg_TypeZ;
    protected Ghost.Pagination.Pagination Paging;
    DataBase db = new DataBase();

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    ViewState["SortDesc"] = true;
    ViewState["SortField_New"] = "TypeID";
    ViewState["SortField_Old"] = "TypeID";
    this.lb_Manage.Text = new ManageInput(Config.TypeZActionUrl + "?Type=TypeZ_Add", Config.TypeZActionUrl).OutPut(true, true, true,true, true, Config.ModalDialogWidth, Config.ModalDialogHeight, "", Config.ale_TypeZDelConfirm, "TypeID", "Type=TypeZ_Modify");
    } if(null != Request.Form[Config.DeleteButtonName] && Request.Form[Config.DeleteButtonName].ToString() == Config.DeleteButtonValue)
    {
    // 删除Y轴类别
    if(Function.DeleteMessageByCheckBoxValue("CompanyTypeZ", "TypeID"))
    {
    Page.Controls.Add(new LiteralControl(Function.AlertScript(Config.ale_TypeZDelSuccess)));
    }
    else
    {
    Page.Controls.Add(new LiteralControl(Function.AlertScript(Config.ale_TypeZDelAbort)));
    }
    }
    // 在此处放置用户代码以初始化页面
    } protected override void OnPreRender(EventArgs e)
    {
    this.BinderDataGrid();
    base.OnPreRender (e);
    } #region DataGrid Client
    public string ShowCheckBox(int CountryID)
    {
    return Function.ShowCheckBox(CountryID);
    }
    public string ShowTypeXYName(int TypeYID)
    {
    return  Function.GetTypeYNameByID(TypeYID);
    }
    #endregion private void BinderDataGrid()
    {
    string sql = "SELECT * FROM CompanyTypeZ ";
    sql += " ORDER BY " + ViewState["SortField_New"].ToString();
    if((bool)ViewState["SortDesc"])
    {
    sql += " Desc";
    }
    SqlCommand sqlcmd = new SqlCommand(sql, db.GetConnection()); this.Paging.Pagination_SqlCommand = sqlcmd;
    DataSet ds = this.Paging.GetCurrentPageDataSet(); this.dg_TypeZ.DataSource = ds;
    this.dg_TypeZ.DataBind();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.dg_TypeZ.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dg_TypeZ_SortCommand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void dg_TypeZ_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    ViewState["SortField_New"] = e.SortExpression;
    if(ViewState["SortField_New"].ToString() == ViewState["SortField_Old"].ToString())
    {
    ViewState["SortDesc"] = !(bool)ViewState["SortDesc"];
    }
    ViewState["SortField_Old"] = ViewState["SortField_New"].ToString();
    string Sort;
    if((bool)ViewState["SortDesc"])
    {
    Sort = "▼";
    }
    else
    {
    Sort = "▲";
    }
    this.dg_TypeZ.Columns[1].HeaderText = "Z轴类别(中文)";
    this.dg_TypeZ.Columns[2].HeaderText = "Z轴类别(英文)";
    this.dg_TypeZ.Columns[3].HeaderText = "X轴类别";
    this.dg_TypeZ.Columns[4].HeaderText = "Y轴类别"; if(e.SortExpression == "TypeZ")
    {
    this.dg_TypeZ.Columns[1].HeaderText = "Z轴类别(中文)" + Sort;
    }
    else if(e.SortExpression == "TypeZEn")
    {
    this.dg_TypeZ.Columns[2].HeaderText = "Z轴类别(英文)" + Sort;
    }
    else if(e.SortExpression == "TypeXID")
    {
    this.dg_TypeZ.Columns[3].HeaderText = "X轴类别" + Sort;
    }
    else if(e.SortExpression == "TypeYID")
    {
    this.dg_TypeZ.Columns[4].HeaderText = "Y轴类别" + Sort;
    }
    }
    }
    }
      

  8.   

    注:楼上得各位 都有缺陷加了where  查询后都会有问题的   (指使用分页时 不管是自动还是手动)
    自己生成sql 语句吧 最灵活  或用proc 我给你个通用的/*--用存储过程实现的分页程序 显示指定表、视图、查询结果的第X页
    对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法
    如果视图或查询结果中有主键,不推荐此方法--邹建 2003.09--*//*--调用示例
    exec p_show '地区资料' exec p_show '地区资料',5,3,'地区编号,地区名称,助记码','地区编号'
    --*/
    /*--调用示例
       use haihuioa
       exec p_show  'select * from clientservicelog where fkid=1 and pdate>=''2003-12-1'' and pdate<=''2003-12-3''      ',5,1,'','pdate asc'
       go
    */
    /*
    因为要顾及通用性,所以对带排序的查询语句有一定要求.如果先排序,再出结果.就是:exec p_show 'select top 100 percent * from 地区资料 order by 地区名称',5,3,'地区编号,地区名称,助记码'
    --查询语句加上:top 100 percent
    */
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_show]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_show]
    GOCREATE Proc p_show
    @QueryStr nvarchar(4000), --表名、视图名、查询语句
    @PageSize int=5, --每页的大小(行数)
    @PageCurrent int=1, --要显示的页
    @FdShow nvarchar (4000)='', --要显示的字段列表,如果查询结果有标识字段,需要指定此值,且不包含标识字段
    @FdOrder nvarchar (1000)='' --排序字段列表
    as
    declare @FdName nvarchar(250) --表中的主键或表、临时表中的标识列名
    ,@Id1 varchar(20),@Id2 varchar(20) --开始和结束的记录号
    ,@Obj_ID int --对象ID
    --表中有复合主键的处理
    declare @strfd nvarchar(2000) --复合主键列表
    ,@strjoin nvarchar(4000) --连接字段
    ,@strwhere nvarchar(2000) --查询条件
    select @Obj_ID=object_id(@QueryStr)
    ,@FdShow=case isnull(@FdShow,'') when '' then ' *' else ' '+@FdShow end
    ,@FdOrder=case isnull(@FdOrder,'') when '' then '' else ' order by '+@FdOrder end
    ,@QueryStr=case when @Obj_ID is not null then ' '+@QueryStr else ' ('+@QueryStr+') a' end--如果显示第一页,可以直接用top来完成
    if @PageCurrent=1
    begin
    select @Id1=cast(@PageSize as varchar(20))
    exec('select top '+@Id1+@FdShow+' from '+@QueryStr+@FdOrder)
    return
    end--如果是表,则检查表中是否有标识更或主键
    if @Obj_ID is not null and objectproperty(@Obj_ID,'IsTable')=1
    begin
    select @Id1=cast(@PageSize as varchar(20))
    ,@Id2=cast((@PageCurrent-1)*@PageSize as varchar(20)) select @FdName=name from syscolumns where id=@Obj_ID and status=0x80
    if @@rowcount=0 --如果表中无标识列,则检查表中是否有主键
    begin
    if not exists(select 1 from sysobjects where parent_obj=@Obj_ID and xtype='PK')
    goto lbusetemp --如果表中无主键,则用临时表处理 select @FdName=name from syscolumns where id=@Obj_ID and colid in(
    select colid from sysindexkeys where @Obj_ID=id and indid in(
    select indid from sysindexes where @Obj_ID=id and name in(
    select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
    )))
    if @@rowcount>1 --检查表中的主键是否为复合主键
    begin
    select @strfd='',@strjoin='',@strwhere=''
    select @strfd=@strfd+',['+name+']'
    ,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
    ,@strwhere=@strwhere+' and b.['+name+'] is null'
    from syscolumns where id=@Obj_ID and colid in(
    select colid from sysindexkeys where @Obj_ID=id and indid in(
    select indid from sysindexes where @Obj_ID=id and name in(
    select name from sysobjects where xtype='PK' and parent_obj=@Obj_ID
    )))
    select @strfd=substring(@strfd,2,2000)
    ,@strjoin=substring(@strjoin,5,4000)
    ,@strwhere=substring(@strwhere,5,4000)
    goto lbusepk
    end
    end
    end
    else
    goto lbusetemp/*--使用标识列或主键为单一字段的处理方法--*/
    lbuseidentity:
    exec('select top '+@Id1+@FdShow+' from '+@QueryStr
    +' where '+@FdName+' not in(select top '
    +@Id2+' '+@FdName+' from '+@QueryStr+@FdOrder
    +')'+@FdOrder
    )
    return/*--表中有复合主键的处理方法--*/
    lbusepk:
    exec('select '+@FdShow+' from(select top '+@Id1+' a.* from
    (select top 100 percent * from '+@QueryStr+@FdOrder+') a
    left join (select top '+@Id2+' '+@strfd+' 
    from '+@QueryStr+@FdOrder+') b on '+@strjoin+'
    where '+@strwhere+') a'
    )
    return/*--用临时表处理的方法--*/
    lbusetemp:
    select @FdName='[ID_'+cast(newid() as varchar(40))+']'
    ,@Id1=cast(@PageSize*(@PageCurrent-1) as varchar(20))
    ,@Id2=cast(@PageSize*@PageCurrent-1 as varchar(20))exec('select '+@FdName+'=identity(int,0,1),'+@FdShow+'
    into #tb from'+@QueryStr+@FdOrder+'
    select '+@FdShow+' from #tb where '+@FdName+' between '
    +@Id1+' and '+@Id2
    )GO