CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

DataGrid无法实现自动刷新

楼主linzhifeng(林知枫)2004-05-01 01:06:47 在 .NET技术 / ASP.NET 提问

在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();  
   
  } 问题点数:20、回复次数:5Top

1 楼popcode(枫.net)回复于 2004-05-01 01:41:24 得分 5

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的例子Top

2 楼linzhifeng(林知枫)回复于 2004-05-01 01:56:40 得分 0

不行啊,这么改后,点击"编辑"按钮后,什么都没有了!  
  数据库连接等等都在第一次打开页面时执行,以后打开页面就不执行了.Top

3 楼ztx998(学习ing)回复于 2004-05-01 08:02:07 得分 5

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

4 楼ztx998(学习ing)回复于 2004-05-01 08:03:28 得分 5

编辑这么写,更新这么写  
   
  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();  
  }Top

5 楼bitm(爱思派)回复于 2004-05-01 08:17:27 得分 5

studyTop

相关问题

  • 如何让IE实现自动刷新?
  • 自动刷新页面如何实现?
  • 怎样实现页面自动刷新?
  • dropdownlist怎样实现自动刷新
  • 求.net自动刷新datagrid代码。
  • xmlhttp与vbscript实现自动刷新?怎么加setTimeout()实现?
  • 各位大虾:怎样实现窗口自动刷新?或定时刷新?---急!
  • 改变窗口大小就自动刷新如何实现
  • 如何实现刷新功能(非自动方式)
  • 怎么实现下载完后自动刷新本页?

关键词

  • datagrid
  • 数据
  • ds
  • source
  • leiming
  • datagrid1
  • strconnection
  • fl
  • 更新
  • 绑定

得分解答快速导航

  • 帖主:linzhifeng
  • popcode
  • ztx998
  • ztx998
  • bitm

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo