DataGrid无法实现自动刷新

linzhifeng 2004-05-01 01:06:47
在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();

}
...全文
79 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bitm 2004-05-01
  • 打赏
  • 举报
回复
study
ztx998 2004-05-01
  • 打赏
  • 举报
回复
编辑这么写,更新这么写

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string zbmc = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string zbgs = ((TextBox)e.Item.Cells[2].Controls[0]).Text;

Conn=new DBServer();
Conn.Open();
string strSql="update zbtable set zbmc='"+zbmc+"',zbgs='"+zbgs+"' where zbbm='"+e.Item.Cells[0].Text+"'";

SqlCommand sqlCommandUpdate = new SqlCommand(strSql,Conn.Connection);
sqlCommandUpdate.ExecuteNonQuery();
Conn.Close();
DataGrid1.EditItemIndex=-1;

mikecatbind();//重新绑定
ShowStats();
Label3.Text="<script language='javascript'>alert('信息已保存')</script>";
}

private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=Convert.ToInt32(e.Item.ItemIndex);
mikecatbind();//重新绑定
ShowStats();
}
ztx998 2004-05-01
  • 打赏
  • 举报
回复
if(!IsPostBack){
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"]);

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();

}
linzhifeng 2004-05-01
  • 打赏
  • 举报
回复
不行啊,这么改后,点击"编辑"按钮后,什么都没有了!
数据库连接等等都在第一次打开页面时执行,以后打开页面就不执行了.
popcode 2004-05-01
  • 打赏
  • 举报
回复
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"]);
放在!IsPostBack里
你可以看看quickstart的例子

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧