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
/// dg_changed_sort 的摘要说明。
17
/// </summary>
18
public class dg_changed_sort : System.Web.UI.Page
19
{
20
protected System.Web.UI.WebControls.DataGrid myDataGrid;
21
protected System.Web.UI.WebControls.Label SqlStatement;
22
protected SqlConnection myconnection = new SqlConnection();
23
protected string strsql = "SELECT CompanyName , ContactName , ContactTitle , Phone , Fax FROM Customers ";
24
DataSet mydataset = new DataSet();
25
26
private void Page_Load(object sender, System.EventArgs e)
27
{
28
// 在此处放置用户代码以初始化页面
29
30
if(!IsPostBack)
31
{
32
binddata();
33
}
34
}
35
private void binddata()
36
{
37
string strconn = "server = localhost;user id =sa;password =;database =Northwind";
38
myconnection.ConnectionString = strconn;
39
strsql = strsql + SqlStatement.Text;
40
41
SqlDataAdapter mycommand = new SqlDataAdapter(strsql,myconnection);
42
mycommand.Fill(mydataset,"customers");
43
44
myDataGrid.DataSource = mydataset.Tables["customers"].DefaultView;
45
myDataGrid.DataBind();
46
}
47
48
Web 窗体设计器生成的代码
70
71
private void myDataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
72
{
73
//这一句很关键,也很值得注意,若" ORDER BY "的两边都要 留空格,否则将会出现错误.
//SqlStatement是一个属性为隐蔵的label
74
SqlStatement.Text=" ORDER BY "+e.SortExpression;
75
binddata();
76
}
77
78
private void myDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
79
{
80
myDataGrid.CurrentPageIndex=e.NewPageIndex;
81
binddata();
82
}
83
}
84
}
85
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
70
71
72
73
//SqlStatement是一个属性为隐蔵的label
74
75
76
77
78
79
80
81
82
83
84
85
浙公网安备 33010602011771号