DataGrid更新数据困惑!update了后台得数据库,但是DataGrid页面没有更新,非得再次编辑状态按下取消,就见到新根新的数据了 !
主要代码如下,愿听详解!
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.sqlDataAdapter1.Fill(dataSet31);
if(!IsPostBack)
{
this.DataGrid1.DataBind();
}
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string UpdateCmd="update maintenance set Disposal_State=@state ,Remark=@remark where ID=@id";
SqlCommand MyCommand=new SqlCommand(UpdateCmd,this.sqlConnection1);
MyCommand.Parameters.Add(new SqlParameter("@state", SqlDbType.Bit,1));
MyCommand.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar,100));
MyCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.BigInt,8));
MyCommand.Parameters["@id"].Value = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
// 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
int NumCols = e.Item.Cells.Count;
CheckBox CurrentCheckBox = (CheckBox)e.Item.Cells[5].Controls[1];
if(CurrentCheckBox.Checked==true)
MyCommand.Parameters["@state"].Value=1;
else
MyCommand.Parameters["@state"].Value=0;
TextBox CurrentTextBox = (TextBox)e.Item.Cells[6].Controls[1];
MyCommand.Parameters["@remark"].Value=CurrentTextBox.Text.ToString();
MyCommand.Connection.Open();
MyCommand.ExecuteNonQuery();
MyCommand.Connection.Close();
//// 完成更新作业后使数据行跳出编辑模式
DataGrid1.EditItemIndex = -1;
// this.sqlDataAdapter1.Update(dataSet31);
DataGrid1.DataBind();
}
}