布局页
Code
aspx 页面中 GridView 布局(共四列数据和一列操作链接,只有后两列数据可以编辑):
<Columns>
<asp:BoundField HeaderText="商品编号" DataField="PID" ReadOnly="true" SortExpression="PID" />
<asp:BoundField HeaderText="供应商编号" DataField="GID" ReadOnly="true" SortExpression="GID" />
<asp:TemplateField HeaderText="商品名称" SortExpression="PName">
<ItemTemplate>
<%#Eval("PName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width="80px" Font-Size="8pt" Font-Names="Tahoma" ID="PName" Text='<%#Eval("PName")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="价格" SortExpression="Price">
<ItemTemplate>
<%#Eval("Price")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Price" runat="server" Font-Names="Tahoma" Font-Size="8pt" Text='<%#Eval("Price")%>' Width="50px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="操作" ShowDeleteButton="True"
ShowSelectButton="True" />
</Columns>
aspx 页面中 GridView 布局(共四列数据和一列操作链接,只有后两列数据可以编辑):
<Columns>
<asp:BoundField HeaderText="商品编号" DataField="PID" ReadOnly="true" SortExpression="PID" />
<asp:BoundField HeaderText="供应商编号" DataField="GID" ReadOnly="true" SortExpression="GID" />
<asp:TemplateField HeaderText="商品名称" SortExpression="PName">
<ItemTemplate>
<%#Eval("PName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width="80px" Font-Size="8pt" Font-Names="Tahoma" ID="PName" Text='<%#Eval("PName")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="价格" SortExpression="Price">
<ItemTemplate>
<%#Eval("Price")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Price" runat="server" Font-Names="Tahoma" Font-Size="8pt" Text='<%#Eval("Price")%>' Width="50px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="操作" ShowDeleteButton="True"
ShowSelectButton="True" />
</Columns>
后置代码页
Code
1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Data.SqlClient;
11
12 public partial class _Default : System.Web.UI.Page
13 {
14 #region GridView 分页及编辑功能!
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Page.Title = "C# GridView 编辑、更新、删除、排序测试程序!";
18
19 //Label lb = new Label();
20 //lb.Text = "测试一下动态添加控件到页面中!";
21 //this.Controls.Add(lb);
22
23 Label l = new Label();
24 l = Label1;
25 l.Font.Size = 8;
26 l.Font.Name = "微软雅黑";
27 l.ForeColor = System.Drawing.Color.Red;
28 }
29
30 protected void LinkButton1_Click(object sender, EventArgs e)
31 {
32 gvBind();
33 }
34
35 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
36 {
37 GridView1.PageIndex = e.NewPageIndex;
38 gvBind();
39 }
40
41 #endregion
42
43 #region 绑定数据到 GridView
44 private void gvBind()
45 {
46 string cns = "Server=.;database=MyDB;uid=sa;pwd=123456";
47 string cmd = "Select * From Product";
48
49 SqlDataAdapter da = new SqlDataAdapter(cmd, cns);
50 DataSet ds = new DataSet("Product");
51
52
53 try
54 {
55 da.Fill(ds, "Product");
56 DataView dv = ds.Tables[0].DefaultView;
57
58 this.GridView1.DataSource = dv;
59 this.GridView1.AllowPaging = true;
60 GridView1.PageSize = 15;
61 this.GridView1.DataBind();
62 }
63 catch (Exception ex)
64 {
65 Label1.Text = ex.Message;
66 }
67 }
68
69 private void gvBind(string sortExpression, string sortDirection)
70 {
71 //重载一次,增加排序功能
72 string cns = "Server=.;database=MyDB;uid=sa;pwd=123456";
73 string cmd = "Select * From Product";
74
75 SqlDataAdapter da = new SqlDataAdapter(cmd, cns);
76 DataSet ds = new DataSet("Product");
77
78 try
79 {
80 da.Fill(ds, "Product");
81 DataView dv = ds.Tables[0].DefaultView;
82
83 //排序关键字
84 dv.Sort = sortExpression + " " + sortDirection;
85
86 this.GridView1.DataSource = dv;
87 this.GridView1.AllowPaging = true;
88 GridView1.PageSize = 15;
89 this.GridView1.DataBind();
90 }
91 catch (Exception ex)
92 {
93 Label1.Text = ex.Message;
94 }
95 }
96 #endregion
97
98 #region 编辑,放弃编辑,刷新页面
99 protected void LinkButton2_Click(object sender, EventArgs e)
100 {
101 Response.Redirect(Request.RawUrl);
102 }
103 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
104 {
105 GridView1.EditIndex = e.NewEditIndex;
106 gvBind();
107 }
108 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
109 {
110 GridView1.EditIndex = -1;
111 gvBind();
112 }
113 #endregion
114
115 #region 更新 GridView 中的数据行!
116 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
117 {
118 //非常变态的一个地方,GridView 必须指定 DataKeyNames,也就是字段名称
119 string pid = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
120 string pn = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("PName")).Text;
121 string pr = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Price")).Text;
122
123 //更新数据库
124 string sql = "Update Product Set PName='" + pn + "',Price=" + pr + " Where PID=" + Convert.ToString(pid);
125 SqlConnection CN = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123456;");
126 SqlCommand CMD = new SqlCommand(sql, CN);
127
128 if (CN.State.ToString() == "Closed") CN.Open();
129 CMD.ExecuteNonQuery();
130 CMD.Dispose();
131 if (CN.State.ToString() == "Open") CN.Close();
132
133 Label1.Text = "更新成功!";
134
135 GridView1.EditIndex = -1; //设为 -1 可取消编辑状态
136 gvBind();
137 }
138 #endregion
139
140 #region 删除 GridView 中的数据行!
141 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
142 {
143 string pid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
144
145 //删除数据库中的记录
146 string sql = "Delete From Product Where PID = " + pid;
147 SqlConnection CN = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123456;");
148 SqlCommand CMD = new SqlCommand(sql, CN);
149
150 if (CN.State.ToString() == "Closed") CN.Open();
151 CMD.ExecuteNonQuery();
152 CMD.Dispose();
153 if (CN.State.ToString() == "Open") CN.Close();
154
155 Label1.Text = "删除成功!";
156
157 gvBind();
158 }
159 #endregion
160
161 #region 选择 GridView 中的数据行!
162 protected void GridView1_SelectChanging(object sender, GridViewSelectEventArgs e)
163 {
164 GridView1.SelectedIndex = e.NewSelectedIndex;
165 gvBind();
166
167 Label1.Text = Convert.ToString(e.NewSelectedIndex);
168 }
169 #endregion
170
171 #region GridView 排序
172 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
173 {
174 //排序问题!
175 ViewState["sortExpression"] = e.SortExpression;
176
177 if ((string)ViewState["sortDirection"] == "ASC" || ViewState["sortDirection"] == null)
178 {
179 ViewState["sortDirection"] = "DESC";
180 gvBind(e.SortExpression, "DESC");
181 }
182 else
183 {
184 ViewState["sortDirection"] = "ASC";
185 gvBind(e.SortExpression, "ASC");
186 }
187 #endregion
188 }
189 }
1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Data.SqlClient;
11
12 public partial class _Default : System.Web.UI.Page
13 {
14 #region GridView 分页及编辑功能!
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 Page.Title = "C# GridView 编辑、更新、删除、排序测试程序!";
18
19 //Label lb = new Label();
20 //lb.Text = "测试一下动态添加控件到页面中!";
21 //this.Controls.Add(lb);
22
23 Label l = new Label();
24 l = Label1;
25 l.Font.Size = 8;
26 l.Font.Name = "微软雅黑";
27 l.ForeColor = System.Drawing.Color.Red;
28 }
29
30 protected void LinkButton1_Click(object sender, EventArgs e)
31 {
32 gvBind();
33 }
34
35 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
36 {
37 GridView1.PageIndex = e.NewPageIndex;
38 gvBind();
39 }
40
41 #endregion
42
43 #region 绑定数据到 GridView
44 private void gvBind()
45 {
46 string cns = "Server=.;database=MyDB;uid=sa;pwd=123456";
47 string cmd = "Select * From Product";
48
49 SqlDataAdapter da = new SqlDataAdapter(cmd, cns);
50 DataSet ds = new DataSet("Product");
51
52
53 try
54 {
55 da.Fill(ds, "Product");
56 DataView dv = ds.Tables[0].DefaultView;
57
58 this.GridView1.DataSource = dv;
59 this.GridView1.AllowPaging = true;
60 GridView1.PageSize = 15;
61 this.GridView1.DataBind();
62 }
63 catch (Exception ex)
64 {
65 Label1.Text = ex.Message;
66 }
67 }
68
69 private void gvBind(string sortExpression, string sortDirection)
70 {
71 //重载一次,增加排序功能
72 string cns = "Server=.;database=MyDB;uid=sa;pwd=123456";
73 string cmd = "Select * From Product";
74
75 SqlDataAdapter da = new SqlDataAdapter(cmd, cns);
76 DataSet ds = new DataSet("Product");
77
78 try
79 {
80 da.Fill(ds, "Product");
81 DataView dv = ds.Tables[0].DefaultView;
82
83 //排序关键字
84 dv.Sort = sortExpression + " " + sortDirection;
85
86 this.GridView1.DataSource = dv;
87 this.GridView1.AllowPaging = true;
88 GridView1.PageSize = 15;
89 this.GridView1.DataBind();
90 }
91 catch (Exception ex)
92 {
93 Label1.Text = ex.Message;
94 }
95 }
96 #endregion
97
98 #region 编辑,放弃编辑,刷新页面
99 protected void LinkButton2_Click(object sender, EventArgs e)
100 {
101 Response.Redirect(Request.RawUrl);
102 }
103 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
104 {
105 GridView1.EditIndex = e.NewEditIndex;
106 gvBind();
107 }
108 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
109 {
110 GridView1.EditIndex = -1;
111 gvBind();
112 }
113 #endregion
114
115 #region 更新 GridView 中的数据行!
116 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
117 {
118 //非常变态的一个地方,GridView 必须指定 DataKeyNames,也就是字段名称
119 string pid = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
120 string pn = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("PName")).Text;
121 string pr = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Price")).Text;
122
123 //更新数据库
124 string sql = "Update Product Set PName='" + pn + "',Price=" + pr + " Where PID=" + Convert.ToString(pid);
125 SqlConnection CN = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123456;");
126 SqlCommand CMD = new SqlCommand(sql, CN);
127
128 if (CN.State.ToString() == "Closed") CN.Open();
129 CMD.ExecuteNonQuery();
130 CMD.Dispose();
131 if (CN.State.ToString() == "Open") CN.Close();
132
133 Label1.Text = "更新成功!";
134
135 GridView1.EditIndex = -1; //设为 -1 可取消编辑状态
136 gvBind();
137 }
138 #endregion
139
140 #region 删除 GridView 中的数据行!
141 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
142 {
143 string pid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
144
145 //删除数据库中的记录
146 string sql = "Delete From Product Where PID = " + pid;
147 SqlConnection CN = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123456;");
148 SqlCommand CMD = new SqlCommand(sql, CN);
149
150 if (CN.State.ToString() == "Closed") CN.Open();
151 CMD.ExecuteNonQuery();
152 CMD.Dispose();
153 if (CN.State.ToString() == "Open") CN.Close();
154
155 Label1.Text = "删除成功!";
156
157 gvBind();
158 }
159 #endregion
160
161 #region 选择 GridView 中的数据行!
162 protected void GridView1_SelectChanging(object sender, GridViewSelectEventArgs e)
163 {
164 GridView1.SelectedIndex = e.NewSelectedIndex;
165 gvBind();
166
167 Label1.Text = Convert.ToString(e.NewSelectedIndex);
168 }
169 #endregion
170
171 #region GridView 排序
172 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
173 {
174 //排序问题!
175 ViewState["sortExpression"] = e.SortExpression;
176
177 if ((string)ViewState["sortDirection"] == "ASC" || ViewState["sortDirection"] == null)
178 {
179 ViewState["sortDirection"] = "DESC";
180 gvBind(e.SortExpression, "DESC");
181 }
182 else
183 {
184 ViewState["sortDirection"] = "ASC";
185 gvBind(e.SortExpression, "ASC");
186 }
187 #endregion
188 }
189 }