[求助]为什么我的datagrid控件无法更新.
到网上搜了很多文章都没有解决,我是新人,大家多多帮忙。代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bindgrid();
}
}
public void Bindgrid()
{
string conn="server=LZH;uid=sa;pwd=sa;database=sms";
SqlConnection myconn=new SqlConnection(conn);
myconn.Open();
string strsql="select Student_id,Student_name,Student_sex,Student_nation,Student_birthday,Student_time,Student_classid,Student_home,Student_else from student";
SqlDataAdapter da=new SqlDataAdapter(strsql,myconn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_student.DataSource=ds;
Dgd_student.DataBind();
myconn.Close();
}
private void Dgd_student_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string conn="server=LZH;uid=sa;pwd=sa;database=sms";
SqlConnection myconn=new SqlConnection(conn);
string strsql="update student set Student_name=@Student_name,Student_sex=@Student_sex,Student_nation=@Student_nation,Student_birthday=@Student_birthday,Student_time=@Student_time,Student_classid=@Student_classid,Student_home=@Student_home,Student_else=@Student_else where Student_id=@Student_id";
SqlCommand cm=new SqlCommand(strsql,myconn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@Student_name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_sex",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_nation",SqlDbType.Char,5));
cm.Parameters.Add(new SqlParameter("@Student_birthday",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_time",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_classid",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_home",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_else",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@Student_name"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@Student_sex"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[3].Controls[0]).Text;
cm.Parameters["@Student_nation"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@Student_birthday"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[5].Controls[0]).Text;
cm.Parameters["@Student_time"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[6].Controls[0]).Text;
cm.Parameters["@Student_classid"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[7].Controls[0]).Text;
cm.Parameters["@Student_home"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[8].Controls[0]).Text;
cm.Parameters["@Student_else"].Value=colvalue;
cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_student.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}