在HTML页里: < asp:GridView ID = " GridView1 " runat = " server " OnPageIndexChanging = " GridView1_PageIndexChanging " AutoGenerateColumns = " False " AllowPaging = " True " PageSize = " 5 " OnRowDeleting = " GridView1_RowDeleting " DataKeyNames = " uid " OnRowCancelingEdit = " GridView1_RowCancelingEdit " OnRowUpdating = " GridView1_RowUpdating " OnRowEditing = " GridView1_RowEditing " > < 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 > < asp:TemplateField HeaderText = " 操作 " ShowHeader = " False " > < EditItemTemplate > < asp:LinkButton ID = " LinkButton1 " runat = " server " CausesValidation = " True " CommandName = " Update " Text = " 更新 " ></ asp:LinkButton > < asp:LinkButton ID = " LinkButton2 " runat = " server " CausesValidation = " False " CommandName = " Cancel " Text = " 取消 " ></ asp:LinkButton > </ EditItemTemplate > < ItemTemplate > < asp:LinkButton ID = " LinkButton1 " runat = " server " CausesValidation = " False " CommandName = " Edit " Text = " 编辑 " ></ asp:LinkButton > < asp:LinkButton ID = " LinkButton3 " runat = " server " CausesValidation = " False " CommandName = " Delete " Text = " 删除 " OnClientClick = " return confirm('确认要删除吗?'); " ></ asp:LinkButton > </ ItemTemplate > </ asp:TemplateField > </ Columns > </ asp:GridView > 在后台代码里: public void GridViewDataBind() ... { 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(); } catch(Exception ex) ...{ ex.ToString(); } } protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e) ... { GridView1.PageIndex = e.NewPageIndex; GridViewDataBind(); } private void AddDummyData(DataSet ds) ... { //如果没有数据显示空行 DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); dt.Rows.Add(newRow); } protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e) ... {//删除行 //得到ID号 string rowToDelete = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string str = "DELETE FROM test where uid=" + "'" + rowToDelete + "'" + ""; SqlDataAdapter da = new SqlDataAdapter(str,conn); DataSet ds = new DataSet(); da.Fill(ds, "customers"); GridView1.DataSource = ds.Tables["customers"]; GridView1.DataBind(); GridViewDataBind(); } protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e) ... {//取消编辑状态 GridView1.EditIndex = -1; GridViewDataBind(); } protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e) ... { string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); string str = "update test set uname=@uname,age=@age,sex=@sex where uid='"+ID+"'"; SqlCommand comm = new SqlCommand(str, conn); SqlParameter parm1 = new SqlParameter("@uname", SqlDbType.VarChar, 50); parm1.Value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text; SqlParameter parm2 = new SqlParameter("@age", SqlDbType.Int); parm2.Value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text; SqlParameter parm3 = new SqlParameter("@sex", SqlDbType.VarChar,50); parm3.Value = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSex")).Text; comm.Parameters.Add(parm1); comm.Parameters.Add(parm2); comm.Parameters.Add(parm3); conn.Open(); comm.ExecuteNonQuery(); conn.Close(); this.GridView1.EditIndex = -1; GridViewDataBind(); } protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e) ... { GridView1.EditIndex = e.NewEditIndex; GridViewDataBind(); }