DataGrid无法实现自动刷新
在DataGrid里建了个EditCommandColumn,点击后有"更新"/"取消"两个按钮,现在更新后,文本框中的数据还是更新前的,不自动变为新数据,为什么?
我已经重新绑定了!
源代码:
private void Page_Load(object sender, System.EventArgs e) {
// 在此处放置用户代码以初始化页面
//连接数据库
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
string DataBase = Server.MapPath("../ShuJu/tpxt_fl.mdb");
strConnection += "Data Source=" + DataBase;
MyConnection = new OleDbConnection(strConnection);
//创建OleDbAdapter对象
string strsql="select * from LeiMing_fl where daleiming='"+"摄影作品"+"'";
MyCommand=new OleDbDataAdapter(strsql,MyConnection);
ds=new DataSet();
//将查询结果填充到DataSet对象中
MyCommand.Fill(ds,"LeiMing_fl");
//将DataGrid的数据源设定为DataSet对象
Source=new DataView(ds.Tables["LeiMing_fl"]);
//实现数据绑定
if(!IsPostBack){
DataGrid1.DataSource=Source;
DataGrid1.DataBind();
}
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {
string sxiaoleiming=((TextBox)e.Item.Cells[1].Controls[0]).Text;
int sid=Convert.ToInt32(e.Item.Cells[0].Text);
Label1.Text=sxiaoleiming+sid.ToString();
string strsql_up="update LeiMing_fl set xiaoleiming='"+sxiaoleiming+"' where id="+sid;
OleDbCommand MyCommand1=new OleDbCommand(strsql_up,MyConnection);
MyCommand1.Connection.Open();
MyCommand1.ExecuteNonQuery();
MyCommand1.Connection.Close();
DataGrid1.EditItemIndex = -1;
DataGrid1.DataSource=Source;
DataGrid1.DataBind();
}