< asp:GridView ID = " GridView1 " runat = " server " OnPageIndexChanging = " GridView1_PageIndexChanging " AutoGenerateColumns = " False " AllowPaging = " True " PageSize = " 5 " DataKeyNames = " uid " > < Columns > < asp:TemplateField HeaderText = " 名称 " SortExpression = " uname " > < EditItemTemplate > < asp:TextBox ID = " TextBox1 " runat = " server " Text = ' <%# Bind("uname") %> ' ></ asp:TextBox > </ EditItemTemplate > < ItemTemplate > <% # DataBinder.Eval(Container.DataItem, " uname " ) %></ a > </ ItemTemplate > </ asp:TemplateField > < asp:TemplateField HeaderText = " 年龄 " SortExpression = " uname " > < EditItemTemplate > < asp:TextBox ID = " txtAge " runat = " server " Text = ' <%# Bind("age") %> ' ></ asp:TextBox > </ EditItemTemplate > < ItemTemplate > < asp:Label ID = " Label2 " runat = " server " Text = ' <%# Bind("age") %> ' ></ asp:Label > </ ItemTemplate > </ asp:TemplateField > < asp:TemplateField HeaderText = " 性别 " SortExpression = " sex " > < EditItemTemplate > < asp:TextBox ID = " txtSex " runat = " server " Text = ' <%# Bind("sex") %> ' ></ asp:TextBox > </ EditItemTemplate > < ItemTemplate > < asp:Label ID = " Label3 " runat = " server " Text = ' <%# Bind("sex") %> ' ></ asp:Label > </ ItemTemplate > </ asp:TemplateField > </ Columns > </ asp:GridView > <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="前一页" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="后一页" /> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="最后一页" /> <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="第一页" /> 后台代码: public void GridViewDataBind() ... { Button1.Enabled = true; Button2.Enabled = true; try ...{ SqlDataAdapter da = new SqlDataAdapter("select * from test", conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); if (ds.Tables[0].Rows.Count == 0) ...{ AddDummyData(ds); } GridView1.DataSource = ds.Tables["customers"]; GridView1.AllowPaging = true; GridView1.PageSize = 5; GridView1.DataBind(); if (GridView1.PageIndex == 0) ...{ Button1.Enabled = false; } if (GridView1.PageIndex == GridView1.PageCount - 1) ...{ Button2.Enabled = false; } } catch(Exception ex) ...{ ex.ToString(); } } protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e) ... { GridView1.PageIndex = e.NewPageIndex; GridViewDataBind(); } protected void Button1_Click( object sender, EventArgs e) ... { GridView1.PageIndex = GridView1.PageIndex - 1; GridViewDataBind(); } protected void Button2_Click( object sender, EventArgs e) ... { GridView1.PageIndex = GridView1.PageIndex + 1; GridViewDataBind(); } protected void Button3_Click( object sender, EventArgs e) ... { GridView1.PageIndex = GridView1.PageCount - 1; GridViewDataBind(); } protected void Button4_Click( object sender, EventArgs e) ... { GridView1.PageIndex = 0; GridViewDataBind(); } private void AddDummyData(DataSet ds) ... { //如果没有数据显示空行 DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); dt.Rows.Add(newRow); }