日期:2014-05-18  浏览次数:21016 次

DataGirdView绑定时出现的奇怪问题!
我在窗体的load事件里面帮定的数据,每次加载的时候正常,但是我在单击某一按钮执行更新数据库的操作后,调用load事件重新绑定数据,显示的是更新的数据。
但是我新建了一个窗体,代码是一样的东西,但是这次调用load事件却不显示更新的数据,显示仍然是旧数据,如果关闭窗体,重新打开显示的又是新数据,非常的奇怪,同样的两个窗体为什么那个就可以,到了一个新窗体里面就不行了,请大家帮忙!
代码:
  //窗体加载时绑定数据
  private void FrmUserManager_Load ( object sender , EventArgs e )
  {
  string SqlStr = "";

  TV_Shopping_DataBaseConnection MyShopping = new TV_Shopping_DataBaseConnection ( );
  SqlConnection MyDataBaseConnection = MyShopping.DataBaseConnectOjbect ( );
  MyDataBaseConnection.Open ( );

  SqlStr = "select userid as 'ID',userName as '姓名',userpurview as '权限',userTrue as '有效',userRemind as '备注'from TV_UserInfo ";
  SqlDataAdapter SelectDataAdapter = new SqlDataAdapter ( SqlStr , MyDataBaseConnection );
  DataSet SelectDataSet = new DataSet ( );
  SelectDataAdapter.Fill ( SelectDataSet , "UserInfo" );
  dataGridView1.DataSource = SelectDataSet.Tables [ "UserInfo" ];
  MyDataBaseConnection.Close ( );
  }
  //更新数据库操作
  TV_Shopping_DataBaseConnection MyShopping = new TV_Shopping_DataBaseConnection ( );
  SqlConnection MyDataBaseConnection = MyShopping.DataBaseConnectOjbect ( );
  MyDataBaseConnection.Open ( );

  SqlStr = "update TV_UserInfo set UserId='" + txtUserId.Text + "',UserName='" + txtUserName.Text + "',UserPurview='"
  + UserPurview.Text + "',UserTrue='" + UserTrue.Text + "',UserRemind='" + UserRemind.Text + "'where Userid='"
  + txtUserId.Text + "'";
  SqlDataAdapter UpdateDataAdapter = new SqlDataAdapter ( SqlStr , MyDataBaseConnection );
  DataSet UpdateDataSet = new DataSet ( );
  UpdateDataAdapter.Fill ( UpdateDataSet , "UserInfo" );
  dataGridView1.DataSource = UpdateDataSet.Tables [ "UserInfo" ];
  MyDataBaseConnection.Close ( );
  this.FrmUserManager_Load ( this , e );//调用load事件重新绑定
以上是可以正常使用窗体中的代码
  //新窗体的load事件
  TV_Shopping_DataBaseConnection MyDataBaseConnection = new TV_Shopping_DataBaseConnection ( );
  SqlConnection MyProductConnection = MyDataBaseConnection.DataBaseConnectOjbect ( );
  MyProductConnection.Open ( );
  SqlStr = "select ProNumber as '编号',ProName as '名称',ProColor as '颜色',ProSpecs as '规格',ProPrice as '价格',ProHelpNum as '助记码',"+ "ProRemind as '备注' from TV_Product";
  SqlDataAdapter MyProductAdapter = new SqlDataAdapter ( SqlStr , MyProductConnection );
  DataSet MyDataSet = new DataSet ( );
  MyProductAdapter.Fill ( MyDataSet , "Product" );
  dataGridView1.DataSource = MyDataSet.Tables [ "Product" ] ;
  MyProductConnection.Close ( );
  //更新数据库代码
  TV_Shopping_DataBaseConnection MyDataBaseConnection = new TV_Shopping_DataBaseConnection ( );
  SqlConnection MyProductConnection = MyDataBaseConnection.DataBaseConnectOjbect ( );
  MyProductConnection.Open ( );

  SqlStr = "update TV_Product set ProName='" + ProName.Text + "',ProPrice=" + ProPrice.Text + ",Pr