C#无法使用更新编辑GridView1_RowUpdating 5C

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class sicao_SC_8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(local);Initial Catalog=SC;User ID=lxk;Password=123";
conn.Open();
string sql = "select * from student";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter dapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dapt.Fill(ds, "student");
GridView1.DataSource = ds.Tables["student"];
GridView1.DataBind();
conn.Close();
dapt.Dispose();

}
//添加
protected void Button1_Click(object sender, EventArgs e)
{
    string stu_name = Name.Text.ToString();
    int stu_number = Convert.ToInt32(Number.Text);
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=(local);Initial Catalog=SC;User ID=lxk;Password=123";
    conn.Open();
    string sql = "insert into student(stu_name,stu_number)values('"+stu_name+"','"+stu_number+"')";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.ExecuteNonQuery();
    Response.Write("<script>alert('添加信息成功!');window.location.href='SC-8.aspx'</script>");
    GridView1.DataBind();
    conn.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
//翻页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.DataBind();
}
//编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    this.DataBind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex =-1;
    this.DataBind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    string stu_name = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    int stu_number = Convert.ToInt32(((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text);
    string sql="UPDATE student set stu_name='"+stu_name+"',stu_number='"+stu_number+"' where id='"+id+"'";
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=(local);Initial Catalog=SC;User ID=lxk;Password=123";
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.ExecuteNonQuery();
    conn.Close();
}
//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string sqlstr = "delete from student where id='" + GridView1.DataKeys[e.RowIndex].Values.ToString() + "'";
    string strcon = "Data Source=(local);Database=SC;User ID=lxk;Password=123";
    SqlConnection sqlcon = new SqlConnection(strcon);
    SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
    sqlcon.Open();
    sqlcom.ExecuteNonQuery();
    Response.Write("<script>alert('删除信息成功!');window.location.href='SC-8.aspx'</script>");
    GridView1.DataBind();
    sqlcon.Close();
}

}

c#

1个回答

绑定数据不应该写在页面加载里 应该放在一个方法中 这是一个错误 因为你更新之后需要页面刷新才能重新加载数据
然后执行完之后加一句GridView1.EditIndex = -1,如何再一次绑定数据
还要你这样代码写了太乱了 一些公共的东西不能把它封装在一个方法吗?

EAison
EAison 然后再一次绑定数据 打错字了
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!