知识点:
1: 手工绑定GridView, 不用SqlDataSource
2: 在前台显示HTML 代码(可以将数据绑定到 literal,然后设置 mode=code)
3: 用DropDownList 作查询条件,在 GridView 上显示.
4: 镶嵌GridView.
5: 一次更新多条记录
前台: testGridView2.aspx


1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="testGridView2.aspx.cs" Inherits="testGridView2" %>
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
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
知识点:<br />
13
1: 手工绑定GridView, 不用SqlDataSource<br />
14
2: 在前台显示HTML 代码(可以将数据绑定到 literal,然后设置 mode=code)<br />
15
3: 用DropDownList 作查询条件,在 GridView 上显示.<br />
16
4: 镶嵌GridView.<br />
17
<br />
18
<br />
19
下面做例子<br />
20
<br />
21
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC"
22
BorderStyle="None" BorderWidth="1px" CellPadding="3" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="au_id">
23
<FooterStyle BackColor="White" ForeColor="#000066" />
24
<Columns>
25
<asp:TemplateField HeaderText="名字">
26
<EditItemTemplate>
27
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("au_fname") %>'></asp:TextBox>
28
</EditItemTemplate>
29
<ItemTemplate>
30
<asp:Label ID="lblfname" runat="server" Text='<%# Eval("au_fname") %>'></asp:Label>
31
</ItemTemplate>
32
</asp:TemplateField>
33
<asp:TemplateField HeaderText="城市">
34
<EditItemTemplate>
35
<asp:TextBox ID="txtcity" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>
36
</EditItemTemplate>
37
<ItemTemplate>
38
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
39
</ItemTemplate>
40
</asp:TemplateField>
41
<asp:TemplateField HeaderText="电话">
42
<EditItemTemplate>
43
<asp:TextBox ID="txtphone" runat="server" Text='<%# Eval("phone") %>'></asp:TextBox>
44
</EditItemTemplate>
45
<ItemTemplate>
46
<asp:Label ID="lblphone" runat="server" Text='<%# Eval("phone") %>'></asp:Label>
47
</ItemTemplate>
48
</asp:TemplateField>
49
<asp:TemplateField HeaderText="显示HTML代码">
50
<ItemTemplate>
51
<asp:Literal ID="Literal1" runat="server" Mode="Encode" Text="<B><I>显示HTML</I></B>"></asp:Literal>
52
</ItemTemplate>
53
</asp:TemplateField>
54
</Columns>
55
<RowStyle ForeColor="#000066" />
56
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
57
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
58
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
59
<PagerSettings Visible="False" />
60
</asp:GridView>
61
<asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" Height="25px" Width="483px">
62
<asp:Button ID="btnfirst" runat="server" Text="首页" CommandArgument="first" OnClick="pagging_click" />
63
<asp:Button ID="btnUp" runat="server" Text="上一页" CommandArgument="up" OnClick="pagging_click" />
64
<asp:Button ID="btnNext" runat="server" Text="下一页" CommandArgument="next" OnClick="pagging_click" />
65
<asp:Button ID="btnEnd" runat="server" Text="页尾" CommandArgument="end" OnClick="pagging_click" /></asp:Panel>
66
<br />
67
<br />
68
=================<br />
69
下面这个是利用一个DropDownList当作查询条件,然后在GridView 中显示<br />
70
<br />
71
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
72
DataTextField="state" DataValueField="state">
73
</asp:DropDownList>
74
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
75
SelectCommand="SELECT DISTINCT [state] FROM [authors]"></asp:SqlDataSource>
76
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4"
77
DataKeyNames="au_id" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None">
78
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
79
<Columns>
80
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
81
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
82
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
83
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
84
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
85
</Columns>
86
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
87
<EditRowStyle BackColor="#999999" />
88
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
89
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
90
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
91
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
92
</asp:GridView>
93
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
94
SelectCommand="SELECT [au_fname], [au_id], [city], [state], [phone] FROM [authors] WHERE ([state] = @state)">
95
<SelectParameters>
96
<asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue"
97
Type="String" />
98
</SelectParameters>
99
</asp:SqlDataSource>
100
<br />
101
=========================<br />
102
下面是一个镶嵌事例.<br />
103
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" BackColor="White"
104
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="emp_id"
105
DataSourceID="SqlDataSource3" OnRowDataBound="GridView3_RowDataBound">
106
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
107
<Columns>
108
<asp:BoundField DataField="emp_id" HeaderText="emp_id" ReadOnly="True" SortExpression="emp_id" />
109
<asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
110
<asp:BoundField DataField="pub_id" HeaderText="pub_id" SortExpression="pub_id" />
111
<asp:TemplateField>
112
<HeaderTemplate>
113
书名
114
</HeaderTemplate>
115
<ItemTemplate>
116
<asp:BulletedList ID="BulletedList1" runat="server">
117
</asp:BulletedList>
118
</ItemTemplate>
119
</asp:TemplateField>
120
</Columns>
121
<RowStyle BackColor="White" ForeColor="#330099" />
122
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
123
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
124
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
125
</asp:GridView>
126
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
127
SelectCommand="SELECT [emp_id], [lname], [pub_id] FROM [employee]"></asp:SqlDataSource>
128
<br />
129
<br />
130
===========================<br />
131
一次更新多条记录<br />
132
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" CellPadding="4"
133
DataKeyNames="au_id" DataSourceID="SqlDataSource4" ForeColor="#333333" GridLines="None" AllowPaging="True">
134
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
135
<Columns>
136
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
137
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
138
<asp:TemplateField HeaderText="编辑该列" Visible="False">
139
<EditItemTemplate>
140
141
</EditItemTemplate>
142
<ItemTemplate>
143
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_fname") %>'></asp:TextBox>
144
</ItemTemplate>
145
<HeaderTemplate>
146
编辑该列
147
</HeaderTemplate>
148
</asp:TemplateField>
149
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
150
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
151
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
152
</Columns>
153
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
154
<EditRowStyle BackColor="#999999" />
155
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
156
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
157
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
158
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
159
</asp:GridView>
160
161
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
162
SelectCommand="SELECT [au_id], [au_fname], [city], [phone], [state] FROM [authors]">
163
</asp:SqlDataSource>
164
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="编辑全部" />
165
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="保存全部" /> <br />
166
<br />
167
<br />
168
<br />
169
<br />
170
</div>
171
</form>
172
</body>
173
</html>
174
后台: testGridView2.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
using System.Data.SqlClient;
12
13
public partial class testGridView2 : System.Web.UI.Page
14

{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
if (!IsPostBack)
18
{
19
bind();
20
}
21
}
22
23
#region
24
/**//// <summary>
25
/// 设置一个绑定类,手工绑定GridView,不用SqlDataSource.
26
/// </summary>
27
private void bind()
28
{
29
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
30
SqlDataAdapter sda = new SqlDataAdapter("select * from [authors]",con);
31
DataSet ds = new DataSet();
32
sda.Fill(ds,"authors");
33
34
GridView1.DataSource=ds.Tables["authors"];
35
//下面设置分页
36
GridView1.AllowPaging = true;
37
GridView1.PageSize = 5;
38
39
//再绑定数据
40
GridView1.DataBind();
41
42
//下面设置分页导航按钮的显示状态.比如:第一页时,上一页与首页不可见.
43
if(GridView1.PageIndex==0)
44
{//页码在首页时,上一页与首页的按钮不显示
45
btnfirst.Visible = false;
46
btnUp.Visible = false;
47
}
48
else
49
{
50
btnfirst.Visible = true;
51
btnUp.Visible = true;
52
}
53
54
if (GridView1.PageIndex == GridView1.PageCount - 1)
55
{//页码在尾页时,下一页与末页的按钮不显示
56
btnNext.Visible = false;
57
btnEnd.Visible = false;
58
}
59
else
60
{
61
btnNext.Visible = true;
62
btnEnd.Visible = true;
63
}
64
65
}
66
#endregion
67
68
#region
69
/**////
70
///设置一个命令cmd,用于对数据的添加,删除,修改等操作.
71
/// 根据参数: que
72
///
73
private int cmd(string que)
74
{
75
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
76
con.Open();
77
SqlCommand cmd = new SqlCommand(que,con);
78
return cmd.ExecuteNonQuery();
79
}
80
#endregion
81
82
#region
83
/**//// <summary>
84
/// 实现分页,注意要设置上一页,下一页,首页,页尾等四个按钮的click事件=pagging_click
85
/// </summary>
86
protected void pagging_click(object sender, EventArgs e)
87
{
88
switch(((Button)sender).CommandArgument.ToString())
89
{//获取 四个按钮的 CommandArgument参数
90
case "first":
91
GridView1.PageIndex=0;
92
break;
93
case "up":
94
GridView1.PageIndex=GridView1.PageIndex-1;
95
break;
96
case "next":
97
GridView1.PageIndex = GridView1.PageIndex + 1;
98
break;
99
case "end":
100
GridView1.PageIndex = GridView1.PageCount - 1;
101
break;
102
}
103
bind();
104
}
105
#endregion
106
107
108
#region
109
/**//// <summary>
110
/// 设置数据行的事件,比如:修改,删除,取消编辑
111
/// </summary>
112
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
113
{//取消编辑 cancel 事件.
114
GridView1.EditIndex = -1;
115
bind();
116
}
117
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
118
{// 删除记录 Delete
119
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
120
string que = "delete from authors where au_id='"+id+"'";
121
if(cmd(que)>0)
122
{
123
bind();
124
}
125
126
}
127
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
128
{//进入编辑模式 Edit
129
GridView1.EditIndex = e.NewEditIndex;
130
bind();
131
}
132
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
133
{//编辑动作 Update , 先取得当前ID, 注意这里的写法(如何取得当前前的字段值):
134
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
135
//注意下面这里,这些必须做在一个模板列里面,要不然是找不到这个"控件"的
136
//显示的列写在 ItemTemple..里面,编辑的列写在 EditTemp
里面
137
string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtname")).Text;
138
string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtcity")).Text;
139
string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtphone")).Text;
140
141
string que = "update authors set au_fname='"+fname+"',city='"+city+"',phone='"+phone+"' where au_id='"+id+"'";
142
if(cmd(que)>0)
143
{// 如果 > 0 ,则编辑成功,退出编辑模式.
144
GridView1.EditIndex = -1;
145
bind();
146
}
147
148
149
//string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();
150
//string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text;
151
/**/////注意上面,我之所以找到了文本框的名字,那是因为我把所有的列都转换成了模板列,不怕你笑,我整了至少一个小时才整出来
152
////但是编辑他的数据根本没有这么麻烦,我们即将学到的DetailsView和FromView可以轻松完成这个任务.
153
////这里我硬着头皮做出来只是因为给有些喜欢玩稀奇的朋友提个思路。
154
//string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text;
155
//string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;
156
//string query = "update authors set au_fname='" + fname + "',city='" + city + "',phone='" + phone + "' where au_id='" + id + "'";
157
//if (cmd(query) > 0)
158
//{
159
// GridView1.EditIndex = -1;
160
// bind();
161
//}
162
163
164
}
165
#endregion
166
167
private SqlDataReader read(string id)
168
{
169
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
170
con.Open();
171
SqlCommand cmd = new SqlCommand("select title from titles where pub_id='" + id + "'", con);
172
//注意下面这句!!
173
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
174
}
175
176
177
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
178
{
179
foreach (GridViewRow gvr in GridView3.Rows)
180
{//跌代GridView1的所有行
181
string id = gvr.Cells[2].Text;
182
//查找到当前行的第三列的文本,把他作为我们的ID,换句话说是作为我们的查询条件
183
BulletedList bl = (BulletedList)gvr.Cells[3].FindControl("BulletedList1");
184
//找到我们安排到第4列的卧底,一个被镶套在里面的BulletedList
185
bl.DataSource = read(id);
186
//将他的数据源指定为我们上面定制的方法,当然,还是得把ID传给他
187
bl.DataTextField = "title";
188
//指定要显示的文本字段
189
bl.DataBind();
190
//我不想说了,这个要是你不知道的话,去死吧你!
191
}
192
}
193
protected void Button1_Click(object sender, EventArgs e)
194
{//编辑全部
195
GridView4.Columns[1].Visible = false;//隐藏显示列
196
GridView4.Columns[2].Visible = true; //显示编辑列
197
}
198
protected void Button2_Click(object sender, EventArgs e)
199
{
200
for (int i = 0; i < GridView4.Rows.Count; i++)
201
{//循环所有的数据行
202
string id = (GridView4.Rows[i].Cells[0].Text).ToString();
203
string fname = ((TextBox)GridView4.Rows[i].Cells[2].FindControl("TextBox1")).Text;
204
//得到当前行的ID和Job_desc列的值
205
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
206
con.Open();
207
SqlCommand cmd = new SqlCommand("update authors set au_fname='" + fname + "' where au_id='" + id + "'", con);
208
cmd.ExecuteNonQuery();
209
//执行更新
210
211
//更新全部。隐藏第3列,显示第2列
212
GridView4.Columns[2].Visible = false;
213
GridView4.Columns[1].Visible = true;
214
}
215
}
216
}
217
1

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="testGridView2.aspx.cs" Inherits="testGridView2" %>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
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
知识点:<br />13
1: 手工绑定GridView, 不用SqlDataSource<br />14
2: 在前台显示HTML 代码(可以将数据绑定到 literal,然后设置 mode=code)<br />15
3: 用DropDownList 作查询条件,在 GridView 上显示.<br />16
4: 镶嵌GridView.<br />17
<br />18
<br />19
下面做例子<br />20
<br />21
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC"22
BorderStyle="None" BorderWidth="1px" CellPadding="3" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="au_id">23
<FooterStyle BackColor="White" ForeColor="#000066" />24
<Columns>25
<asp:TemplateField HeaderText="名字">26
<EditItemTemplate>27
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("au_fname") %>'></asp:TextBox>28
</EditItemTemplate>29
<ItemTemplate>30
<asp:Label ID="lblfname" runat="server" Text='<%# Eval("au_fname") %>'></asp:Label>31
</ItemTemplate>32
</asp:TemplateField>33
<asp:TemplateField HeaderText="城市">34
<EditItemTemplate>35
<asp:TextBox ID="txtcity" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>36
</EditItemTemplate>37
<ItemTemplate>38
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>39
</ItemTemplate>40
</asp:TemplateField>41
<asp:TemplateField HeaderText="电话">42
<EditItemTemplate>43
<asp:TextBox ID="txtphone" runat="server" Text='<%# Eval("phone") %>'></asp:TextBox>44
</EditItemTemplate>45
<ItemTemplate>46
<asp:Label ID="lblphone" runat="server" Text='<%# Eval("phone") %>'></asp:Label>47
</ItemTemplate>48
</asp:TemplateField>49
<asp:TemplateField HeaderText="显示HTML代码">50
<ItemTemplate>51
<asp:Literal ID="Literal1" runat="server" Mode="Encode" Text="<B><I>显示HTML</I></B>"></asp:Literal>52
</ItemTemplate>53
</asp:TemplateField>54
</Columns>55
<RowStyle ForeColor="#000066" />56
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />57
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />58
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />59
<PagerSettings Visible="False" />60
</asp:GridView>61
<asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" Height="25px" Width="483px">62
<asp:Button ID="btnfirst" runat="server" Text="首页" CommandArgument="first" OnClick="pagging_click" />63
<asp:Button ID="btnUp" runat="server" Text="上一页" CommandArgument="up" OnClick="pagging_click" />64
<asp:Button ID="btnNext" runat="server" Text="下一页" CommandArgument="next" OnClick="pagging_click" />65
<asp:Button ID="btnEnd" runat="server" Text="页尾" CommandArgument="end" OnClick="pagging_click" /></asp:Panel>66
<br />67
<br />68
=================<br />69
下面这个是利用一个DropDownList当作查询条件,然后在GridView 中显示<br />70
<br />71
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"72
DataTextField="state" DataValueField="state">73
</asp:DropDownList>74
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"75
SelectCommand="SELECT DISTINCT [state] FROM [authors]"></asp:SqlDataSource>76
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" CellPadding="4"77
DataKeyNames="au_id" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None">78
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />79
<Columns>80
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />81
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />82
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />83
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />84
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />85
</Columns>86
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />87
<EditRowStyle BackColor="#999999" />88
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />89
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />90
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />91
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />92
</asp:GridView>93
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"94
SelectCommand="SELECT [au_fname], [au_id], [city], [state], [phone] FROM [authors] WHERE ([state] = @state)">95
<SelectParameters>96
<asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue"97
Type="String" />98
</SelectParameters>99
</asp:SqlDataSource>100
<br />101
=========================<br />102
下面是一个镶嵌事例.<br />103
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" BackColor="White"104
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="emp_id"105
DataSourceID="SqlDataSource3" OnRowDataBound="GridView3_RowDataBound">106
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />107
<Columns>108
<asp:BoundField DataField="emp_id" HeaderText="emp_id" ReadOnly="True" SortExpression="emp_id" />109
<asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />110
<asp:BoundField DataField="pub_id" HeaderText="pub_id" SortExpression="pub_id" />111
<asp:TemplateField>112
<HeaderTemplate>113
书名114
</HeaderTemplate>115
<ItemTemplate>116
<asp:BulletedList ID="BulletedList1" runat="server">117
</asp:BulletedList>118
</ItemTemplate>119
</asp:TemplateField>120
</Columns>121
<RowStyle BackColor="White" ForeColor="#330099" />122
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />123
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />124
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />125
</asp:GridView>126
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"127
SelectCommand="SELECT [emp_id], [lname], [pub_id] FROM [employee]"></asp:SqlDataSource>128
<br />129
<br />130
===========================<br />131
一次更新多条记录<br />132
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" CellPadding="4"133
DataKeyNames="au_id" DataSourceID="SqlDataSource4" ForeColor="#333333" GridLines="None" AllowPaging="True">134
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />135
<Columns>136
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />137
<asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />138
<asp:TemplateField HeaderText="编辑该列" Visible="False">139
<EditItemTemplate>140
141
</EditItemTemplate>142
<ItemTemplate>143
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("au_fname") %>'></asp:TextBox>144
</ItemTemplate>145
<HeaderTemplate>146
编辑该列147
</HeaderTemplate>148
</asp:TemplateField>149
<asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />150
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />151
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />152
</Columns>153
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />154
<EditRowStyle BackColor="#999999" />155
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />156
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />157
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />158
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />159
</asp:GridView>160
161
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"162
SelectCommand="SELECT [au_id], [au_fname], [city], [phone], [state] FROM [authors]">163
</asp:SqlDataSource>164
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="编辑全部" />165
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="保存全部" /> <br />166
<br />167
<br />168
<br />169
<br />170
</div>171
</form>172
</body>173
</html>174

后台: testGridView2.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
using System.Data.SqlClient;12

13
public partial class testGridView2 : System.Web.UI.Page14


{15
protected void Page_Load(object sender, EventArgs e)16

{17
if (!IsPostBack)18

{19
bind();20
}21
}22

23

#region24

/**//// <summary>25
/// 设置一个绑定类,手工绑定GridView,不用SqlDataSource.26
/// </summary>27
private void bind()28

{29
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);30
SqlDataAdapter sda = new SqlDataAdapter("select * from [authors]",con);31
DataSet ds = new DataSet();32
sda.Fill(ds,"authors");33

34
GridView1.DataSource=ds.Tables["authors"];35
//下面设置分页36
GridView1.AllowPaging = true;37
GridView1.PageSize = 5;38
39
//再绑定数据40
GridView1.DataBind();41

42
//下面设置分页导航按钮的显示状态.比如:第一页时,上一页与首页不可见.43
if(GridView1.PageIndex==0)44

{//页码在首页时,上一页与首页的按钮不显示45
btnfirst.Visible = false;46
btnUp.Visible = false;47
}48
else49

{50
btnfirst.Visible = true;51
btnUp.Visible = true;52
}53

54
if (GridView1.PageIndex == GridView1.PageCount - 1)55

{//页码在尾页时,下一页与末页的按钮不显示56
btnNext.Visible = false;57
btnEnd.Visible = false;58
}59
else60

{61
btnNext.Visible = true;62
btnEnd.Visible = true;63
}64

65
}66
#endregion67

68

#region69

/**////70
///设置一个命令cmd,用于对数据的添加,删除,修改等操作.71
/// 根据参数: que 72
///73
private int cmd(string que)74

{75
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);76
con.Open();77
SqlCommand cmd = new SqlCommand(que,con);78
return cmd.ExecuteNonQuery();79
}80
#endregion81

82

#region83

/**//// <summary>84
/// 实现分页,注意要设置上一页,下一页,首页,页尾等四个按钮的click事件=pagging_click85
/// </summary>86
protected void pagging_click(object sender, EventArgs e)87

{88
switch(((Button)sender).CommandArgument.ToString())89

{//获取 四个按钮的 CommandArgument参数90
case "first":91
GridView1.PageIndex=0;92
break;93
case "up":94
GridView1.PageIndex=GridView1.PageIndex-1;95
break;96
case "next":97
GridView1.PageIndex = GridView1.PageIndex + 1;98
break;99
case "end":100
GridView1.PageIndex = GridView1.PageCount - 1;101
break;102
}103
bind();104
}105
#endregion106

107

108

#region109

/**//// <summary>110
/// 设置数据行的事件,比如:修改,删除,取消编辑
111
/// </summary>112
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)113

{//取消编辑 cancel 事件.114
GridView1.EditIndex = -1;115
bind();116
}117
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)118

{// 删除记录 Delete119
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();120
string que = "delete from authors where au_id='"+id+"'";121
if(cmd(que)>0)122

{123
bind();124
}125

126
}127
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)128

{//进入编辑模式 Edit129
GridView1.EditIndex = e.NewEditIndex;130
bind();131
}132
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)133

{//编辑动作 Update , 先取得当前ID, 注意这里的写法(如何取得当前前的字段值):134
string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();135
//注意下面这里,这些必须做在一个模板列里面,要不然是找不到这个"控件"的136
//显示的列写在 ItemTemple..里面,编辑的列写在 EditTemp
里面137
string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtname")).Text;138
string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtcity")).Text;139
string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtphone")).Text;140

141
string que = "update authors set au_fname='"+fname+"',city='"+city+"',phone='"+phone+"' where au_id='"+id+"'";142
if(cmd(que)>0)143

{// 如果 > 0 ,则编辑成功,退出编辑模式.144
GridView1.EditIndex = -1;145
bind();146
}147

148

149
//string id = GridView1.DataKeys[e.RowIndex]["au_id"].ToString();150
//string fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1")).Text;151

/**/////注意上面,我之所以找到了文本框的名字,那是因为我把所有的列都转换成了模板列,不怕你笑,我整了至少一个小时才整出来152
////但是编辑他的数据根本没有这么麻烦,我们即将学到的DetailsView和FromView可以轻松完成这个任务.153
////这里我硬着头皮做出来只是因为给有些喜欢玩稀奇的朋友提个思路。154
//string city = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox2")).Text;155
//string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;156
//string query = "update authors set au_fname='" + fname + "',city='" + city + "',phone='" + phone + "' where au_id='" + id + "'";157
//if (cmd(query) > 0)158
//{159
// GridView1.EditIndex = -1;160
// bind();161
//}162

163

164
}165
#endregion166

167
private SqlDataReader read(string id)168

{169
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);170
con.Open();171
SqlCommand cmd = new SqlCommand("select title from titles where pub_id='" + id + "'", con);172
//注意下面这句!!173
return cmd.ExecuteReader(CommandBehavior.CloseConnection);174
}175

176

177
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)178

{179
foreach (GridViewRow gvr in GridView3.Rows)180

{//跌代GridView1的所有行181
string id = gvr.Cells[2].Text;182
//查找到当前行的第三列的文本,把他作为我们的ID,换句话说是作为我们的查询条件183
BulletedList bl = (BulletedList)gvr.Cells[3].FindControl("BulletedList1");184
//找到我们安排到第4列的卧底,一个被镶套在里面的BulletedList185
bl.DataSource = read(id);186
//将他的数据源指定为我们上面定制的方法,当然,还是得把ID传给他187
bl.DataTextField = "title";188
//指定要显示的文本字段189
bl.DataBind();190
//我不想说了,这个要是你不知道的话,去死吧你!191
}192
}193
protected void Button1_Click(object sender, EventArgs e)194

{//编辑全部195
GridView4.Columns[1].Visible = false;//隐藏显示列196
GridView4.Columns[2].Visible = true; //显示编辑列197
}198
protected void Button2_Click(object sender, EventArgs e)199

{200
for (int i = 0; i < GridView4.Rows.Count; i++)201

{//循环所有的数据行202
string id = (GridView4.Rows[i].Cells[0].Text).ToString();203
string fname = ((TextBox)GridView4.Rows[i].Cells[2].FindControl("TextBox1")).Text;204
//得到当前行的ID和Job_desc列的值205
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);206
con.Open();207
SqlCommand cmd = new SqlCommand("update authors set au_fname='" + fname + "' where au_id='" + id + "'", con);208
cmd.ExecuteNonQuery();209
//执行更新210

211
//更新全部。隐藏第3列,显示第2列212
GridView4.Columns[2].Visible = false;213
GridView4.Columns[1].Visible = true;214
}215
}216
}217

浙公网安备 33010602011771号