原来写的 你看看
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;/***************************  DataGrid控件的分页和排序   **************/
/*
 * 功能 => DataGrid 控件的分页和排序 利用ViewState存储排序表达式,
 *        
 *        因为ViewState不占服务器资源,所以不放在Session中,但是ViewState是发回給
 *   客户端的,最好放些"小"内容,所以用ViewState存储排序表达式,而 DataView则
 *   用Session 存储
 * 
 * 说明 => 排序功能最好放在 DataGrid控件的 Pre_Render事件中
 *         Pre_Render事件是DataGrid控件呈现給用户前所发生的事件 
 * 时间 => 2003/6/13   创建人 => stpangpang
 * 
*/
/********************************************************************/namespace ass
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
//先设定,排序的方向为 ASC 放在 ViewState 中
ViewState["SortDirection"]="ASC";
this.BindGrid();
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.PreRender += new System.EventHandler(this.DataGrid1_PreRender);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void BindGrid()
{
SqlConnection conn=new SqlConnection("server=localhost;database=northwind;uid=northwind;pwd=northwind;");
conn.Open();
SqlCommand cmd=new SqlCommand("select employeeid,firstname from employees",conn);
//SqlDataReader reader=cmd.ExecuteReader();

SqlDataAdapter adapter=new SqlDataAdapter("select employeeid ,firstname from employees",conn);
DataSet DS=new DataSet();
adapter.Fill(DS);
Session["DV"]=DS.Tables[0].DefaultView;

} private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
this.DataGrid1.CurrentPageIndex=0;
//属性SortField 存促排序的字段
SortField=e.SortExpression; //重新绑定DataGrid控件
BindGrid();
} private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;

} private void DataGrid1_PreRender(object sender, System.EventArgs e)
{
//判断排序方向
if(ViewState["SortDirection"].ToString()=="ASC")
{
//如果是升序就改为降序
ViewState["SortDirection"]="DESC";
}
else
{
//如果是降序就改为升序
ViewState["SortDirection"]="ASC";
}
//排序字段是 属性SortField存储的字段
DataView DV=(DataView)Session["DV"];
DV.Sort=SortField;

try
{
//设定,排序表达式
DV.Sort=SortField + " " + ViewState["SortDirection"].ToString();
}
catch
{
//不作任何处理,因为第一次加载,SortField 的属性是空(String.Empty) 会有错误 
}
this.DataGrid1.DataSource=DV;
this.DataGrid1.DataBind(); } //属性,SortField 用ViewState跟踪排序字段
string SortField
{
get
{
object o=ViewState["SortField"];

if(o==null)
{
//如果为空,返回空字符串
return String.Empty;
}
return (string)ViewState["SortField"];
}
set
{
ViewState["SortField"]=value;

}
}
}
}

解决方案 »

  1.   

    用Session把派序字段记录下来
    很简单啊楼上得是用ViewState都差不多啦
      

  2.   

    如果是一个页面中页面的传值 ,用viewstate ,如果是不同的页面就要用session 或cache了 ~~ 具体情况具体对待,不是一概而论的 ~~
      

  3.   

    1  SqlFunction.inc<%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <Script Languate="VB" Runat="Server">
      'strSQL 为 SQL 语句,DataBaseName 为数据库文件名,TableName 为数据表名称。
      Function CreateDataSet(strSQL As String, DataBaseName As String, TableName As String) As DataSet
        Dim ConnString As String = "Data Source=localhost;Integrated Security=SSPI;" & _
                                   "Initial Catalog=" & DataBaseName
        Dim objCmd As New SqlDataAdapter(strSQL, ConnString)
        Dim DS As New DataSet()
        objCmd.Fill(DS, TableName)  
        CreateDataSet = DS
      End Function
    </Script>
    2<!-- #include File="SqlFunction.inc" -->
    <Script Language="VB" Runat="Server">
      'BindList 过程用来指定数据源,并连接到 DataGrid 控件
      Sub BindList()
        Dim DS As DataSet = CreateDataSet("Select * From 零部件报价表", "Price", "零部件报价表")
        Dim DV As New DataView(DS.Tables("零部件报价表"))
        DV.Sort = SortField.Value
        myDataGrid.DataSource = DV
        myDataGrid.DataBind()
      End Sub
     
      Sub Page_Load(sender As Object, e As Eventargs)
        If Not IsPostBack Then BindList()
      End Sub  Sub DataGrid_OnSortCommand(sender As Object, e As DataGridSortCommandEventArgs)
        If InStr(SortField.Value, "Desc") = 0 Then
          SortField.Value = e.SortExpression & " Desc"
        Else
          SortField.Value = e.SortExpression & " Asc"
        End If
        BindList()
      End Sub</Script><Html>
      <Body>
        <H1 Align="Center">电脑零部件报价系统</H1>
        <Form Runat="Server">
          <Asp:DataGrid Runat="Server" Id="myDataGrid" AutoGenerateColumns="True" 
           HorizontalAlign="Center" AllowSorting="True" OnSortCommand="DataGrid_OnSortCommand">        <HeaderStyle Font-Size="Small" Font-Bold="True" HorizontalAlign="Center" ForeColor="#FFFFCC" BackColor="#990000" />
            <ItemStyle Font-Size="X-Small" ForeColor="#330099" />      </Asp:DataGrid>
          <Input Type="Hidden" Runat="Server" Id="SortField" Value="零部件种类">
        </Form>
      </Body>
    </Html>你也可以通过Request.QueringString(@变量名)来传送排序参数
      

  4.   

    我们这个所写的排序,其实是对 dataview 排序(对你所点击的字段 排序) ~~ 再帮定到datagrid上面的,
      

  5.   

    "我们这个所写的排序,其实是对 dataview 排序(对你所点击的字段 排序) ~~ 再帮定到datagrid上面的,"
    这我知道,你写的是所有的字段公用一个排序状态(升序或是降序),我想要写个每个字段都是独立的,即我无论点一个字段几次,不会影响其他字段的排序方式。private void DataGrid1_PreRender(object sender, System.EventArgs e)
    {
    //判断排序方向
    if(ViewState["SortDirection"].ToString()=="ASC")
    {
    //如果是升序就改为降序
    ViewState["SortDirection"]="DESC";
    }
    else
    {
    //如果是降序就改为升序
    ViewState["SortDirection"]="ASC";
    }
    //排序字段是 属性SortField存储的字段
    DataView DV=(DataView)Session["DV"];
    DV.Sort=SortField;
    try
    {
    //设定,排序表达式
    DV.Sort=SortField + " " + ViewState["SortDirection"].ToString();
    }
    catch
    {
    //不作任何处理,因为第一次加载,SortField 的属性是空(String.Empty) 会有错误 
    }
    this.DataGrid1.DataSource=DV;
    this.DataGrid1.DataBind(); }
    你这样写的话,无论点什么字段,都是升序,降序,升序,降序,升序,降序...这样有规律的排列下去。