protected void Page_Load(Object sender, EventArgs e) 
    {
        myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");        if (!IsPostBack)
            BindGrid("au_id");
    }    protected void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) 
    {
        BindGrid(e.SortExpression);
    }    public void BindGrid(String sortfield) 
    {
        SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);        DataSet ds = new DataSet();
        myCommand.Fill(ds, "作者");        DataView Source = ds.Tables["作者"].DefaultView;
        Source.Sort = sortfield;        MyDataGrid.DataSource=Source;
        MyDataGrid.DataBind();
    }

解决方案 »

  1.   

    参考孟子E章的
    http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=E5254FD8-252F-457C-F61E-32EE353E8BF2
      

  2.   

    首先要设置页眉按钮,然后对按钮添加事件。其实datagrid中有allowsortting属性,但是我用没有什么效果,最后手动写代码点击按钮重新查询,按照规则排序。但是在win form中datagrid就不用写什么就可以实现此功能。我的方法同服务器交互的太频繁不是好办法。等待更好的。只是跟你说一下,没有更好的就这么用吧
      

  3.   

    DragableXpStyleTable.aspx<%@ Page language="c#" Codebehind="DragableXpStyleTable.aspx.cs"
    AutoEventWireup="false" Inherits="eMeng.Exam.DragableXpStyleTable.DragableXpStyleTable" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
      <HEAD>
        <META http-equiv="content-type" content="text/html; charset=gb2312">
        <title>XP 风格的可拖动列、排序的DataGrid的例子</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <link rel="stylesheet" type="text/css" href="xpTable.css">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
        <form id="DragableXpStyleTable" method="post" runat="server">
          <div align="center" style="padding:20px"><b>XP 风格的可拖动列、排序的DataGrid的例子。</b></div>
          <asp:DataGrid id="xpTable" runat="server" AutoGenerateColumns="False" Cellpadding="2"
           BorderWidth="0" BorderStyle="None" STYLE="BORDER-COLLAPSE:separate" Width="90%" CssClass="xpTable">
            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
            <Columns>
              <asp:BoundColumn ItemStyle-Height="22" DataField="Title" SortExpression="Title"
               HeaderText="文章标题"
               HeaderStyle-Font-Bold="True"></asp:BoundColumn>
              <asp:BoundColumn DataField="CreateDate" SortExpression="CreateDate"
               HeaderText="创建日期" HeaderStyle-Font-Bold="True"
               DataFormatString="{0:yyyy年MM月dd日 HH:mm:ss}"></asp:BoundColumn>
              <asp:BoundColumn DataField="SubTitle" SortExpression="SubTitle"
               HeaderText="所属栏目" HeaderStyle-Font-Bold="True"></asp:BoundColumn>
            </Columns>
          </asp:DataGrid>
        </form>
      </body>
    </HTML>DragableXpStyleTable.aspx.csusing 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.OleDb;namespace eMeng.Exam.DragableXpStyleTable
    {
    /// <summary>
    /// DragableXpStyleTable 的摘要说明。
    /// XP风格的可拖动的DataGrid
    /// </summary>
    public class DragableXpStyleTable : System.Web.UI.Page
    {
      protected System.Web.UI.WebControls.DataGrid xpTable; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
       xpTable.Attributes.Add("altRowColor","oldlace");
       xpTable.Attributes.Add("align","center");
       string cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        cnString +=  System.Web.HttpContext.Current.Server.MapPath("TestTable.mdb")
       OleDbConnection cn = new OleDbConnection(cnString);
       cn.Open();
       cnString = "SELECT TOP 15 D.Title,D.CreateDate,S.Title AS SubTitle ";
        cnString += "FROM Document D,Subject S WHERE D.pid=S.id ORDER BY D.CreateDate DESC";
       OleDbCommand cmd = new OleDbCommand(cnString,cn);
       xpTable.DataSource=cmd.ExecuteReader(CommandBehavior.CloseConnection);
       xpTable.DataBind();
       cmd.Dispose();
       cn.Close();
       cn.Dispose();
       cn = null;
    } #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.Load += new System.EventHandler(this.Page_Load);  }
    #endregion
    }
      

  4.   

    我的后台是用的VB,帮我用VB写一下好吗!