前台: testGridView.aspx
1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="testGridView.aspx.cs" Inherits="testGridView" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
<style type="text/css">
9
td{
}{
10
font-size:12px;
11
}
12
</style>
13
</head>
14
<body>
15
<form id="form1" runat="server">
16
<div>
17
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2" AutoGenerateColumns="False"
18
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
19
CellPadding="3" DataKeyNames="id" DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
20
<FooterStyle BackColor="White" ForeColor="#000066" />
21
<Columns>
22
<asp:CommandField HeaderText="编辑" ShowDeleteButton="True" ShowEditButton="True" />
23
<asp:BoundField DataField="id" HeaderText="用户ID" InsertVisible="False" ReadOnly="True"
24
SortExpression="id" />
25
<asp:BoundField DataField="username" HeaderText="用户名" SortExpression="username" >
26
<ItemStyle BackColor="LightSkyBlue" BorderColor="#FFC0C0" BorderStyle="Dotted" ForeColor="Navy" />
27
</asp:BoundField>
28
<asp:BoundField DataField="TJname" HeaderText="性别" SortExpression="sex" />
29
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
30
<asp:BoundField DataField="intro" HeaderText="介绍" SortExpression="intro" />
31
<asp:BoundField DataField="submitTime" HeaderText="加入日期" SortExpression="submitTime" DataFormatString="{0:y}" HtmlEncode="False"/>
32
<asp:TemplateField HeaderText="注册时间">
33
<ItemTemplate>
34
<asp:Label ID="subTime" runat="server" Text='<%# Bind("submitTime", "{0:d}") %>'></asp:Label>
35
</ItemTemplate>
36
</asp:TemplateField>
37
<asp:CheckBoxField DataField="vip" HeaderText="vip会员" ShowHeader="False" SortExpression="vip" />
38
<asp:TemplateField HeaderText="自定义(Sex)">
39
<ItemTemplate>
40
<asp:Label ID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("sex"))==1?"推荐":"不推荐" %>'></asp:Label>
41
</ItemTemplate>
42
</asp:TemplateField>
43
<asp:TemplateField>
44
<HeaderTemplate>
45
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged"
46
Text="全选" />
47
</HeaderTemplate>
48
<ItemTemplate>
49
<asp:CheckBox ID="itemChk" runat="server" Text=""/>
50
</ItemTemplate>
51
</asp:TemplateField>
52
<asp:TemplateField HeaderText="此列显示HTML代码">
53
<ItemTemplate>
54
<asp:Literal ID="Literal1" runat="server" Mode="Encode" Text="<B><p>Html Code</p></B>"></asp:Literal>
55
</ItemTemplate>
56
</asp:TemplateField>
57
</Columns>
58
<RowStyle ForeColor="#000066" />
59
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
60
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
61
<HeaderStyle BackColor="#006699" Font-Bold="False" ForeColor="#FFFFC0" Font-Size="13px" />
62
<EmptyDataTemplate>
63
没有数据吗
64
</EmptyDataTemplate>
65
</asp:GridView>
66
<br />
67
共<asp:Label ID="Label2" runat="server" Text=""><%=GridView1.PageCount %></asp:Label>页,当前是第<asp:Label
68
ID="Label3" runat="server" Text=""><%=GridView1.PageIndex+1 %></asp:Label>页.<br />
69
<br />
70
<asp:DropDownList ID="DropDownList1" runat="server">
71
</asp:DropDownList>
72
73
<asp:Button ID="btnYuc" runat="server" OnClick="btnYuc_Click" Text="隐藏选定的列" />
74
75
<asp:Button ID="btnShow" runat="server" OnClick="btnShow_Click" Text="显示全部列" />
76
77
<asp:Button ID="btnDele" runat="server" Text="删除所选的列" OnClientClick=" return window.confirm('女人 ');" OnClick="btnDele_Click" /><br />
78
<br />
79
www.verycd.com
80
81
</div>
82
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:digConnectionString %>"
83
SelectCommand="select *, TJname=(CASE when sex=1 then '男人' when sex=0 then '女人' end) from test" DeleteCommand="DELETE FROM [test] WHERE [id] = @id" InsertCommand="INSERT INTO [test] ([id], [username], [sex], [age], [intro], [submitTime], [vip]) VALUES (@id, @username, @sex, @age, @intro, @submitTime, @vip)" UpdateCommand="UPDATE [test] SET [username] = @username, [sex] = @sex, [age] = @age, [intro] = @intro, [submitTime] = @submitTime, [vip] = @vip WHERE [id] = @id">
84
<DeleteParameters>
85
<asp:Parameter Name="id" Type="Int32" />
86
</DeleteParameters>
87
<UpdateParameters>
88
<asp:Parameter Name="username" Type="String" />
89
<asp:Parameter Name="sex" Type="Byte" />
90
<asp:Parameter Name="age" Type="Int32" />
91
<asp:Parameter Name="intro" Type="String" />
92
<asp:Parameter Name="submitTime" Type="DateTime" />
93
<asp:Parameter Name="vip" Type="Boolean" />
94
<asp:Parameter Name="id" Type="Int32" />
95
</UpdateParameters>
96
<InsertParameters>
97
<asp:Parameter Name="id" Type="Int32" />
98
<asp:Parameter Name="username" Type="String" />
99
<asp:Parameter Name="sex" Type="Byte" />
100
<asp:Parameter Name="age" Type="Int32" />
101
<asp:Parameter Name="intro" Type="String" />
102
<asp:Parameter Name="submitTime" Type="DateTime" />
103
<asp:Parameter Name="vip" Type="Boolean" />
104
</InsertParameters>
105
</asp:SqlDataSource>
106
107
</form>
108
</body>
109
</html>
110
后台: testGridView.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
public partial class testGridView : System.Web.UI.Page
13

{
14
protected void Page_Load(object sender, EventArgs e)
15
{
16
if (!IsPostBack)
17
{
18
for (int i = 1; i <= GridView1.Columns.Count;i++ )
19
{
20
DropDownList1.Items.Add(i.ToString());
21
}
22
}
23
}
24
public string returnSex(int i)
25
{
26
return i==0?"女":"男";
27
28
29
}
30
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
31
{
32
//效果:增加鼠标所在行的样式.
33
//先判断是否是数据行
34
if(e.Row.RowType==DataControlRowType.DataRow)
35
{
36
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#EFEFEF'");
37
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c;");
38
}
39
}
40
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
41
{
42
//效果:特定列的样式,比如你的哪一个列的样式
43
for (int i = 0; i < GridView1.Rows.Count;i++)
44
{
45
//注意这里的写法,取得绑定的某列的值,要转换为字符串型.
46
//写法: Convert.String(DataBinder.Eval(e.Row.DataItem,"字段名"));
47
string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem,"userName"));
48
if (lbl == "DotNet")
49
{
50
e.Row.BackColor = System.Drawing.Color.LightBlue; //指定颜色.
51
}
52
}
53
54
}
55
protected void btnYuc_Click(object sender, EventArgs e)
56
{
57
int clo=int.Parse(DropDownList1.SelectedValue)-1;
58
/**//*这样写有问题,当你点了一列隐藏后,再隐藏其他列,原来的列还是隐藏的
59
60
GridView1.Columns[clo].Visible = false;
61
62
*/
63
//下面这方法是每一种状态下,最多只能隐藏一个列
64
//如果第二次隐藏列,原来被隐藏的列又显示回来
65
66
for (int i = 0; i < GridView1.Columns.Count;i++ )
67
{
68
if (i == clo)
69
{
70
GridView1.Columns[i].Visible = false;
71
}
72
else
73
{
74
GridView1.Columns[i].Visible = true;
75
}
76
}
77
78
}
79
protected void btnShow_Click(object sender, EventArgs e)
80
{
81
//显示全部列.
82
for(int i=0;i<GridView1.Columns.Count;i++)
83
{
84
GridView1.Columns[i].Visible = true;
85
}
86
}
87
88
protected void chkAll_CheckedChanged(object sender, EventArgs e)
89
{
90
//注意这里的写法/
91
//CheckBox chkAll = (CheckBox)GridView1.Cells[10].FindControl("chkAll");
92
CheckBox chkAll = (CheckBox)GridView1.HeaderRow.Cells[10].FindControl("chkAll");
93
94
if (chkAll.Checked)
95
{
96
foreach (GridViewRow gr in GridView1.Rows)
97
{
98
CheckBox chk = (CheckBox)gr.Cells[10].FindControl("itemChk");
99
if (!chk.Checked)//如果没选中,就让它选中
100
{
101
chk.Checked = true;
102
}
103
/**/////else //这里我把它注释掉了,因为加了的话,选中的会被不选中了
104
////{
105
//// chk.Checked = false;
106
////}
107
}
108
}
109
else //取消全选
110
{
111
foreach (GridViewRow gr in GridView1.Rows)
112
{
113
CheckBox chk = (CheckBox)gr.Cells[10].FindControl("itemChk");
114
chk.Checked = false;
115
}
116
}
117
}
118
119
protected void btnDele_Click(object sender, EventArgs e)
120
{
121
string id = "";
122
foreach(GridViewRow gr in GridView1.Rows)
123
{
124
CheckBox chk = (CheckBox)gr.Cells[10].FindControl("itemChk");
125
if(chk.Checked) //如果选中,则删除
126
{
127
if (id == "")
128
{
129
id += GridView1.DataKeys[gr.DataItemIndex].Value.ToString();
130
}
131
else
132
{
133
id += "," + GridView1.DataKeys[gr.DataItemIndex].Value.ToString();
134
}
135
}
136
}
137
// Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", " alert('" + id + "')");
138
Response.Write("您要删除的记录是:"+id);
139
}
140
}


1



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

后台: testGridView.aspx.cs


1

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

130

131

132



133

134

135

136

137

138

139

140
