1using System;
 2using System.Collections;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Web;
 7using System.Web.SessionState;
 8using System.Web.UI;
 9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12
13namespace changedpage
14{
15    /// <summary>
16    /// sort_datagrid 的摘要说明。
17    /// </summary>

18    public class sort_datagrid : System.Web.UI.Page
19    {
20        protected System.Web.UI.WebControls.DataGrid DataGrid1;
21        protected SqlConnection myconnection = new SqlConnection();
22        string strsql = "select top 10 CustomerID,CompanyName,ContactName,ContactTitle,Phone,Fax from Customers";
23    
24        private void Page_Load(object sender, System.EventArgs e)
25        {
26            if(!IsPostBack)
27            {
28                BindData();
29            }

30        }

31
32        protected void BindData()
33        {
34            string strconn="server=localhost;user id=sa;password=;database=Northwind";            
35
36myconnection.ConnectionString=strconn;
37            SqlCommand mycommand = new SqlCommand(strsql,myconnection);
38            myconnection.Open();
39            SqlDataReader myreader = mycommand.ExecuteReader();
40            DataGrid1.DataSource=myreader;
41            DataGrid1.DataBind();
42            myreader.Close();
43            myconnection.Close();
44            
45        }

46
47        #region Web 窗体设计器生成的代码
48        override protected void OnInit(EventArgs e)
49        {
50            //
51            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
52            //
53            InitializeComponent();
54            base.OnInit(e);
55        }

56        
57        /// <summary>
58        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
59        /// 此方法的内容。
60        /// </summary>

61        private void InitializeComponent()
62        {    
63            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler
64
65(this.DataGrid1_SortCommand);
66            this.Load += new System.EventHandler(this.Page_Load);
67
68        }

69        #endregion

70
71        private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
72        {
73            strsql = strsql+" order by "+ e.SortExpression.ToString();
74            BindData();
75        }

76    }

77}