DataGrid中数据的删除问题

storm97 2003-08-02 12:38:17
我的程序大致如下:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
this.GridBindData();
}

}
private void GridBindData()
{
try
{
GridEdit.Database.InitConnectionString();
this.sqlconn=GridEdit.Database.Connection;
if(this.sqlconn==null)
{
this.sqlconn.Open();
}
//开始邦定数据
string strSql="select * from Employee";
//this.sqlconn.ConnectionString=strSql;
SqlDataAdapter da=new SqlDataAdapter(strSql,this.sqlconn);
ds=new DataSet();
da.Fill(ds,"Employee");
da.Dispose();
//this.DataGrid1.Columns[0].
this.DataGrid1.DeleteCommand +=new System.Web.UI.WebControls.DataGridCommandEventHandler(DeleteData);
this.DataGrid1.ItemDataBound +=new System.Web.UI.WebControls.DataGridItemEventHandler(bound);
this.DataGrid1.DataSource=ds.Tables["Employee"];
this.DataGrid1.DataBind();
}
catch(Exception ex)
{
Response.Write("<script language='javascript'>alert("+ ex.Message +"');</script>");
}
finally
{
if(this.sqlconn!=null)
{
this.sqlconn.Close();
}
}
}
private void DeleteData(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ds.Tables["Employee"].Rows[e.Item.ItemIndex].Delete();
DataView dv=new DataView(ds.Tables["Employee"],null,null,DataViewRowState.Unchanged);
this.DataGrid1.DataSource=dv;
this.DataGrid1.DataBind();

}

问题是在DataGrid中点击删除以后,执行DeleteData()方法,但是DataGrid中显示的删除后的Table的数据不对,当我删除第一行后,显示的是对的,但再点新的第一行的删除,则不能删,但点新的第二行的删除就能删掉第一行的数据,但是第一次山的数据就会显示出来,请问是怎么搞得?
...全文
55 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
storm97 2003-08-02
  • 打赏
  • 举报
回复
没用
redant0 2003-08-02
  • 打赏
  • 举报
回复
假定和视图帮订,我删除一般是这样:
dv[dbGrid.CurrentRowIndex].Delete();
redant0 2003-08-02
  • 打赏
  • 举报
回复
执行ds.AcceptChanges();
storm97 2003-08-02
  • 打赏
  • 举报
回复
执行了,但是还是不行!
redant0 2003-08-02
  • 打赏
  • 举报
回复
执行AcceptChanges
awjx 2003-08-02
  • 打赏
  • 举报
回复
这么长的程序,谁会有时间看呀,还是靠自己吧!
cl_03 2003-08-02
  • 打赏
  • 举报
回复
删除数据是要用Select方法选取未删除的行集,然后再进行操作


DataTable dtdel=new DataTable();
dtdel=(DataTable)this.dgrid_erpair.DataSource;
DataRow[] drdel=dtdel.Select("","",DataViewRowState.CurrentRows);
if(drdel.Length>0)
{
this.diares=MessageBox.Show(this,"真的要删除当前记录吗?","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
switch(this.diares)
{
case DialogResult.Yes:
drdel[this.dgrid_erpair.CurrentCell.RowNumber].Delete();
this.b_save=false;
break;
case DialogResult.No:
return;
}
}
else
{
MessageBox.Show(this,"请选择要删除的记录!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
tomsx 2003-08-02
  • 打赏
  • 举报
回复
VS中的在线帮助是很有用的,你可以自己参考一下,上面有许多同步的帮助
我好久都没有用ASP.NET了,你还是看看帮助吧

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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