我HTML中有两个DataGrid,ID分别为DataGrid1,DataGrid2,
下面是我的CS:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
BindGrid();
BindGrid1();
}
}
public void BindGrid()
{
string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
MyConn = new OleDbConnection(Connectionstr);
string MyCommand = "select * from price";
OleDbDataAdapter dr = new OleDbDataAdapter(MyCommand,MyConn);
DataSet ds = new DataSet();
dr.Fill(ds,"price");
DataGrid1.DataSource=ds.Tables["price"].DefaultView;
DataGrid1.DataBind();
}
public void BindGrid1()
{
string Conn = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
MyConn = new OleDbConnection(Conn);
string MyCommand = "select * from price1";
OleDbDataAdapter dr = new OleDbDataAdapter(MyCommand,MyConn);
DataSet ds = new DataSet();
dr.Fill(ds,"price1");
DataGrid2.DataSource=ds.Tables["price1"].DefaultView;
DataGrid2.DataBind();
}
public ICollection CreateDataSource () 
{
string Connectionstr ="provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb"); 
MyConn = new OleDbConnection(Connectionstr);
OleDbDataAdapter rd = new OleDbDataAdapter(sql,MyConn); 
DataSet ds = new DataSet();
rd.Fill(ds,"price");
DataView MyView = ds.Tables["price"].DefaultView;
return MyView;
}
public ICollection CreateDataSource1 () 
{
string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
MyConn = new OleDbConnection(Connectionstr);
OleDbDataAdapter rd = new OleDbDataAdapter(sql,MyConn); 
DataSet ds = new DataSet();
rd.Fill(ds,"price1");
DataView MyView = ds.Tables["price1"].DefaultView;
return MyView;
}
public void Page_Grid(Object source, DataGridPageChangedEventArgs e) 
{
sql = "select * from price";
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}
public void Page_Grid2(Object source, DataGridPageChangedEventArgs e) 
{
sql = "select * from price1";
DataGrid2.CurrentPageIndex = e.NewPageIndex;
DataGrid2.DataSource = CreateDataSource1();
DataGrid2.DataBind();
}
public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e) 
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();

        }
public void DataGrid2_Edit(Object sender, DataGridCommandEventArgs e) 
{
DataGrid2.EditItemIndex = (int)e.Item.ItemIndex;

BindGrid();
}
public void DataGrid2_Update(Object sender, DataGridCommandEventArgs e) 
{   
BindGrid1();
string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
MyConn = new OleDbConnection(Connectionstr);
MyCommand = new OleDbCommand( "update price1 set design ='"+e.Item.Cells[1].Text+"',content='"+e.Item.Cells[2].Text+"',price='"+e.Item.Cells[3].Text+"' where id="+e.Item.Cells[0].Text ,MyConn);
MyConn.Open();
MyCommand.ExecuteNonQuery();
MyConn.Close();
BindGrid1();
DataGrid2.DataBind();
Response.Redirect(Request.RawUrl);

}
public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e) 
{
string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
MyConn = new OleDbConnection(Connectionstr);
MyCommand = new OleDbCommand( "update price set design ='"+e.Item.Cells[1].Text+"',content='"+e.Item.Cells[2].Text+"',price='"+e.Item.Cells[3].Text+"' where id="+e.Item.Cells[0].Text ,MyConn);
MyConn.Open();
MyCommand.ExecuteNonQuery();
MyConn.Close();
DataGrid1.DataBind();
Response.Redirect(Request.RawUrl);
}

public void DataGrid2_Delete(Object sender, DataGridCommandEventArgs e) 
{
 BindGrid1();
 string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb"); 
 MyConn = new OleDbConnection(Connectionstr);
 MyCommand = new OleDbCommand("delete * from price1 where id="+e.Item.Cells[0].Text,MyConn);
 MyConn.Open();
 MyCommand.ExecuteNonQuery();
 MyConn.Close();
 
}
public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e) 
{

     BindGrid();
 string Connectionstr = "provider = Microsoft.Jet.OLEDB.4.0;Data Source ="+Server.MapPath("Price.mdb");
 MyConn = new OleDbConnection(Connectionstr);
 MyCommand = new OleDbCommand( "delete design,content,price from price where id="+e.Item.Cells[0].Text,MyConn);
 MyConn.Open();
 MyCommand.ExecuteNonQuery();
 MyConn.Close();
 
 }
我的问题是:1:对DataGrid1进行编辑的时候,填上要更新的数据,按“更新”数据库中的数据都为空,数据更新不到数据库,且原始数据都被空了。是不是按“编译”的时候,数据变成了空字符,更新时把空字符更新进去。
           2:对DataGrid2进行编辑的时候,需要按两次“编辑”按钮,才触发编辑事件,然后填上需要更新的数据,按“更新”,数据库中数据不变,但不变成空字符。
           3:不论是按DataGrid1或者DataGrid2的“delete”,都需要按两次才触发删除事件。
大家帮我看看那里错了。

解决方案 »

  1.   

    其中DataGrid1和DataGrid2中的分页,删除,更新,编译事件代码的写法都一样的
      

  2.   

    在事件里重新 DataGrid1.DataBind();
      

  3.   

    应该是没有取到值,你把e.Item.Cells[1].Text等的值用LABEL显示出来看看是什么?
      

  4.   

    事件里重新绑定  但是不是绑定在更新前 是更新后当你触发事件的时候按照你写的是先绑定后运行 这样更新不了 2个DataGrid逻辑上别混淆了!