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
namespace DataGridTest
13

{
14
/**//// <summary>
15
/// WebForm1 的摘要说明。
16
/// </summary>
17
public class WebForm1 : System.Web.UI.Page
18
{
19
protected System.Web.UI.WebControls.Button Button1;
20
protected System.Web.UI.WebControls.HyperLink HyperLink1;
21
protected System.Web.UI.WebControls.DataGrid DataGrid1;
22
23
private void Page_Load(object sender, System.EventArgs e)
24
{
25
if(!this.IsPostBack)
26
{
27
this.BindToDataGrid();
28
}
29
30
// 在此处放置用户代码以初始化页面
31
}
32
private void BindToDataGrid()
33
{
34
SqlConnection con=DB.createCon();
35
SqlDataAdapter sda=new SqlDataAdapter(); //数据适配器
36
sda.SelectCommand=new SqlCommand("select * from employees",con);
37
DataSet ds=new DataSet();
38
sda.Fill(ds,"emp");
39
this.DataGrid1.DataSource=ds.Tables["emp"];
40
this.DataGrid1.DataBind();
41
}
42
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
43
override protected void OnInit(EventArgs e)
44
{
45
//
46
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
47
//
48
InitializeComponent();
49
base.OnInit(e);
50
}
51
52
/**//// <summary>
53
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
54
/// 此方法的内容。
55
/// </summary>
56
private void InitializeComponent()
57
{
58
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
59
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
60
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
61
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
62
this.Button1.Click += new System.EventHandler(this.Button1_Click);
63
this.Load += new System.EventHandler(this.Page_Load);
64
65
}
66
#endregion
67
68
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
69
{
70
}
71
72
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
73
{
74
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
75
this.BindToDataGrid();
76
}
77
78
private void Button1_Click(object sender, System.EventArgs e)
79
{
80
if(DataGrid1.Columns[1].Visible==true)
81
{
82
this.DataGrid1.Columns[1].Visible=false;
83
}
84
else
85
{
86
this.DataGrid1.Columns[1].Visible=true;
87
}
88
}
89
90
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
91
{
92
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
93
{ //添加脚本,使鼠标移动到项上发生变化
94
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#eeeeee'");
95
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
96
}
97
}
98
99
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
100
{//列值进行排序
101
if(ViewState["Order"]==null)
102
{
103
ViewState["Order"]="ASC";
104
}
105
else
106
{
107
if(ViewState["Order"].ToString()=="ASC")
108
{
109
ViewState["Order"]="DESC";
110
}
111
else
112
{
113
ViewState["Order"]="ASC";
114
}
115
116
}
117
//数据绑定显示
118
SqlConnection con=DB.createCon();
119
SqlDataAdapter sda=new SqlDataAdapter(); //数据适配器
120
sda.SelectCommand=new SqlCommand("select * from employees",con);
121
DataSet ds=new DataSet();
122
sda.Fill(ds,"emp");
123
ds.Tables["emp"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString();
124
this.DataGrid1.DataSource=ds.Tables["emp"].DefaultView;
125
this.DataGrid1.DataBind();
126
}
127
}
128
}
129

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

78

79



80

81



82

83

84

85



86

87

88

89

90

91



92

93



94

95

96

97

98

99

100



101

102



103

104

105

106



107

108



109

110

111

112



113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129
