详解GridView自带的编辑删除更新功能

  using System;

  using System.Collections.Generic;

  using System.Configuration;

  using System.Data;

  using System.Data.SqlClient;

  using System.Linq;

  using System.Web;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  public partial class gridView_bianjidelete : System.Web.UI.Page

  {//清清月儿http://blog.csdn.net/21aspnet

  SqlConnection sqlcon;

  SqlCommand sqlcom;

  string strCon = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;

  protected void Page_Load(object sender, EventArgs e)

  {

  if (!IsPostBack)

  {

  bind();

  }

  }

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

  {

  GridView1.EditIndex = e.NewEditIndex;

  bind();

  }

  //删除之后重新绑定

  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

  {

  string sqlstr = "delete from product where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

  sqlcon = new SqlConnection(strCon);

  sqlcom = new SqlCommand(sqlstr, sqlcon);

  sqlcon.Open();

  sqlcom.ExecuteNonQuery();

  sqlcon.Close();

  GridView1.DataBind();

  bind();

  }

  //更新

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

  {

  sqlcon = new SqlConnection(strCon);

  string sqlstr = "update product set name='"

  + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',stock='"

  + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where id='"

  + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

  sqlcom = new SqlCommand(sqlstr, sqlcon);

  sqlcon.Open();

  sqlcom.ExecuteNonQuery();

  sqlcon.Close();

  GridView1.EditIndex = -1;

  // GridView1.DataBind();

  bind();

  }

  //取消

  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

  {

  GridView1.EditIndex = -1;

  bind();

  }

  //绑定

  public void bind()

  {

  string sqlstr = "select * from product p,Uuser u where p.userid=u.id";

  sqlcon = new SqlConnection(strCon);

  SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

  DataSet myds = new DataSet();

  sqlcon.Open();

  myda.Fill(myds, "datatable");

  GridView1.DataSource = myds;

  GridView1.DataKeyNames = new string[] { "id" };//主键

  GridView1.DataBind();

  sqlcon.Close();

  }

  }