各位好,我今天在写程序时,对于DataGrid有一个奇怪问题,老是搞不懂,请各位帮帮忙.问题是这样的,我用DataGrid的"更新"按钮,当我连接SQL Server 2000数据库时,当我按下"更新"按钮时,能够修改数据库的数据,并将更新数据显示在页面,但当我连接Access数据库时,当我按下"更新"按钮时,能够修改数据库的数据,但在页面显示时,不能显示更新的数据,必须刷新一下页面,才能显示修改后的数据,不知是怎么回事,我用的都是同一段代码,只是连接的数据库不同而已,请知道原因的帮我看一下,下面是我的源代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
 
using System.Configuration;
using System.Web.Security ;namespace ShopBookApp
{
 
public class ShopCart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.Label Label2; public static string proidid;  

private void Page_Load(object sender, System.EventArgs e)
{
 
                       if(!IsPostBack)            
                        {
BindGrid();
}
}    
private void MyDataGrid_Edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{   
if(e.CommandName=="Edit")
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}
} private void MyDataGrid_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{   
if(e.CommandName =="Update")
 {
   TableCell itemCel2=e.Item.Cells[2];
   proidid=itemCel2.Text;    TextBox quantityTxt;
quantityTxt = (TextBox) e.Item.Cells[3].Controls[0];
                   
string qua=quantityTxt.Text;
 
                                        
OleDbConnection myConnection2 = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
myConnection2.Open();
OleDbCommand myCommand2 = new OleDbCommand("update Users Set username=@username where logname=@logname", myConnection2);
 
            
myCommand2.Parameters.Add(new OleDbParameter("@username",OleDbType.VarChar)); 
myCommand2.Parameters["@username"].Value =qua;
myCommand2.Parameters.Add(new OleDbParameter("@logname",OleDbType.VarChar)); 
myCommand2.Parameters["@logname"].Value =proidid;  
                                         
  try
{
                         
myCommand2.ExecuteNonQuery();
DataGrid1.EditItemIndex =-1;
BindGrid(); }
catch 
{
 

}
 
} private void MyDataGrid_Cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{   
if(e.CommandName =="Cancel")
{
DataGrid1.EditItemIndex =-1;
BindGrid();
}
}
protected void BindGrid()
{
OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);
OleDbCommand myCommand = new OleDbCommand("select logname,username from Users", myConnection);
 
myConnection.Open();  
OleDbDataReader result = myCommand.ExecuteReader();
DataGrid1.DataSource =result;
DataGrid1.DataBind();
result.Close();
myConnection.Close(); }        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex ;
BindGrid();
}  
}
}

解决方案 »

  1.   

    在连接asscee时很有可能是你点button时没有重新绑定,自己慢慢看下
      

  2.   

    有啊,我有绑定啊,上面的myCommand2.ExecuteNonQuery();
        DataGrid1.EditItemIndex =-1;
        BindGrid();//这一句就是重新绑定啊
      

  3.   

    连接没有关闭try
    {
                             
    myCommand2.ExecuteNonQuery();
                                                  myConnection2.Close();
    DataGrid1.EditItemIndex =-1;
    BindGrid(); }
    catch 
    {
     

      

  4.   

    真是太谢谢amandag(高歌)你了,真的是连接没有关闭,终于解决了,决定给你加分.谢谢