我正在使用gridview和SqlDataSource将数据表信息绑定到gridview。 在gridview更新事件中,我有以下代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var ID = (int)GridView1.DataKeys[e.RowIndex]["No"];
string costring = "the conn string";
string query = "UPDATE mytable SET Age = @Age WHERE No = " + ID;
using (SqlConnection dataConnection = new SqlConnection(costring))
{
using (SqlCommand com = new SqlCommand(query, dataConnection))
{
dataConnection.Open();
int valueID = 18;
com.Parameters.AddWithValue("Age", valueID);
com.ExecuteNonQuery();
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
dataConnection.Close();
}
}
}
答案 0 :(得分:3)
如果发出此声明,是否真的需要再次分配数据源
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
并使用
SqlDataSource1.Update();
有一个使用数据源的例子,我希望这对你有用Datasource example