日期:2014-05-17  浏览次数:20519 次

gridview编辑之后更新不了
public partial class 输入成绩 : System.Web.UI.Page
{
   
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack)
  {
  GridView1.DataBind();
  }

  }
  //获取数据绑定在GV1上
   
   
  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
  DataClassesDataContext ctx = new DataClassesDataContext();
  var products = from p in ctx.stu_mark
  where p.ID == (int)GridView1.SelectedValue
  select p;

  GridView1.DataSource = products;
  GridView1.DataBind();

  }
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
  GridView1.EditIndex = e.NewEditIndex;
  GridView1.DataBind();

  }
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
  DataClassesDataContext db = new DataClassesDataContext();

  var strid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  stu_mark ff = db.stu_mark.Single(b => b.ID == (strid));
  ff.name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
  ff.number = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
  ff.course = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
  ff.credit = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
  ff.mark = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
  db.SubmitChanges();
  GridView1.EditIndex = -1;
  GridView1.DataBind();
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  Response.Redirect("Login.aspx");
  Response.End();
  }
}

------解决方案--------------------
db.SubmitChanges();
GridView1.EditIndex = -1;
GridView1.DataBind();

你都没重新取数据当然没更新的数据了,
------解决方案--------------------
应该结贴!!!
------解决方案--------------------
C# code

 if (!Page.IsPostBack)
  {
   //我没见过有人这么写数据绑定,这里应该写一个完整的数据绑定方法而不是单独的 GridView1.DataBind();

   GridView1.DataBind();改为databind();

  }
//比如下面是你绑定数据的完整方法
public void databind()
{
   DataClassesDataContext ctx = new DataClassesDataContext();
  var products = from p in ctx.stu_mark
  where p.ID == (int)GridView1.SelectedValue
  select p;

  GridView1.DataSource = products;
  GridView1.DataBind();


}