1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
13
namespace 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
protected 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("CustomerID");
29
}
30
}
31
32
protected void BindData(string SortOrder)
33
{
34
string strconn="server=localhost;user id=sa;password=;database=Northwind";
35
myconnection.ConnectionString=strconn;
36
SqlDataAdapter mycommand = new SqlDataAdapter(strsql,myconnection);
37
38
39
DataSet mydataset = new DataSet();
40
mycommand.Fill(mydataset,"Customers1");
41
mydataset.Tables["Customers1"].DefaultView.Sort=SortOrder ;
42
DataGrid1.DataSource=mydataset.Tables["Customers1"].DefaultView;
43
DataGrid1.DataBind();
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
string SortOrder = e.SortExpression.ToString();
74
BindData(SortOrder);
75
}
76
}
77
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
浙公网安备 33010602011771号