一个datatable的问题,关于dt.Rows.Remove 的用法

jiangchuandong 2005-05-26 11:22:37
我前面用dt.Rows.Remove (i)一行数据

DataTable dt_D = dt.GetChanges(DataRowState.Deleted);

但是无法获取delete的行?
完整的代码是
if(MyBox.Checked)
{
dt.Rows.Remove(dt.Rows[i]);
//dt.Rows.Add(dt.NewRow());
}
}
ViewState["source"] = dt;
this.dg_Modify.DataSource = ViewState["source"];
this.dg_Modify.DataBind();

DataTable dt = (DataTable)ViewState["source"];

DataTable dt_D = dt.GetChanges(DataRowState.Deleted);//该dt_D为空
...全文
1017 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeusvenus 2005-05-26
  • 打赏
  • 举报
回复
楼主把REMOVE方法和DELETE方法搞混了,二者是不一样的,你Remove掉Delete当然读不到了。
okkk 2005-05-26
  • 打赏
  • 举报
回复
已经回答了,
itflying 2005-05-26
  • 打赏
  • 举报
回复
不会,datagrid,还需要进一步的学习,帮忙顶一下
hchxxzx 2005-05-26
  • 打赏
  • 举报
回复
应如楼主所言.
zhongwanli 2005-05-26
  • 打赏
  • 举报
回复
Remove()是彻底删除,

Delete() 是标志为 删除。

楼主对比就知道了
exboy 2005-05-26
  • 打赏
  • 举报
回复
先要明白 Table.Rows.Remove( i ) 和 Table.Rows[i].Delete( ) 之间的区别:
Table.Rows.Remove 方法是把一行从 Rows 集合中删除
Table.Rows[i].Delete( ) 方法只是把一个行的状态设置为删除状态,但并没有从 Rows 集合中给删除。

所以当你调用 Table.Rows.Remove( i ) 方法后,在调用 Table.GetChanges 方法来取得删除的行,肯定是取不到被删除的行,而使用Table.Rows[i].Delete( )方法删除行就可以取到。
Richardhu 2005-05-26
  • 打赏
  • 举报
回复
Detached
exboy 2005-05-26
  • 打赏
  • 举报
回复
在 foreach(DataRow dr in dt_D.Rows) 这一句前面加一句
dt_D.AcceptChanges( );
hchxxzx 2005-05-26
  • 打赏
  • 举报
回复
你上面写的有问题,改如下:
strSql = "use " + wuser.CurrentCorpDB + " delete AssetDeptBorrow_detail where nvrAssetBorrowNo = '" + txb_Number.Text.ToString() + "' and " + nvrAssetNo = '" + dr[0].ToString().Trim() + "' ";

jiangchuandong 2005-05-26
  • 打赏
  • 举报
回复
很急,帮忙看看
用了dt.Rows[i].Delete( );DataTable dt_D = dt.GetChanges(DataRowState.Deleted);
后无法用
dr[0].ToString()
是怎么回事?
jiangchuandong 2005-05-26
  • 打赏
  • 举报
回复
谢谢exboy,但是我改了以后用
foreach(DataRow dr in dt_D.Rows)
{
strSql = "use "+wuser.CurrentCorpDB +" delete AssetDeptBorrow_detail where nvrAssetBorrowNo='"+txb_Number.Text.ToString() +"' and "+"nvrAssetNo='" + dr[0].ToString().Trim()+"'";
}
就抱错误
System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row. at System.Data.DataRow.GetDefaultRecord() at System.Data.DataRow.GetRecordFromVersion(DataRowVersion version) at System.Data.DataRow.get_Item(DataColumn column, DataRowVersion version) at System.Data.DataRow.get_Item(DataColumn column) at System.Data.DataRow.get_Item(Int32 columnIndex) at CECERP.IE.Asset.AssetTransfer.AssetDeptBorrow.Save()
exboy 2005-05-26
  • 打赏
  • 举报
回复
不要使用dt.Rows.Remove(dt.Rows[i]);
而是使用dt.Rows[i].Delete( );
jiangchuandong 2005-05-26
  • 打赏
  • 举报
回复
那我怎么才能取到dt.Rows.Remove(dt.Rows[i]);的datatable呢?
jiangchuandong 2005-05-26
  • 打赏
  • 举报
回复
Detached 可以取到這個datatable吗?
dbfC#DataGridView中的常用技巧 只列出技巧部分,后面会有补充 0(最基本的技巧). 获取某列中的某行(某单元格)中的内容 this.currentposition = this.dataGridView1.BindingContext [this.dataGridView1.DataSource, this.dataGridView1.DataMember].Position; bookContent = this.database.dataSet.Tables[0].Rows [this.currentposition][21].ToString().Trim(); MessageBox.Show(bookContent); 1、自定义列 //定义列宽 this.dataGridView1.Columns[0].Width = 80; this.dataGridView1.Columns[1].Width = 80; this.dataGridView1.Columns[2].Width = 180; this.dataGridView1.Columns[3].Width = 120; this.dataGridView1.Columns[4].Width = 120; Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance Host Controls in Windows Forms DataGridView Cells 继承 DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn 生成新的Column类,并指定 CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。 2、自动适应列宽 Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control Samples: DataGridView.AutoSizeColumns( DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows); DataGridView.AutoSizeColumn( DataGridViewAutoSizeColumnCriteria.HeaderOnly, 2, false); DataGridView.AutoSizeRow( DataGridViewAutoSizeRowCriteria.Columns, 2, false); DataGridView.AutoSizeRows( DataGridViewAutoSizeRowCriteria.HeaderAndColumns, 0, dataGridView1.Rows.Count, false); 3、可以绑定并显示对象 Bind Objects to Windows Forms DataGridView Controls 4、可以改变表格线条风格 Change the Border and Gridline Styles in the Windows Forms DataGridView Control Samples: this.dataGridView1.GridColor = Color.BlueViolet; this.dataGridView1.BorderStyle = BorderStyle.Fixed3D; this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; this.dataGridView1.RowHeadersBorderStyle = DataGridVie
using System; using System.Data; using System.Text; namespace ZKXP.BLL { /// /// OnLine 的摘要说明。 /// public class OnLine { private string USERNAME; private int OFFLINEDIFF; private int REMOVEDIFF; public OnLine() { //直接在这里设定、或从配置文件中读取配置参数 ///存放用户名的Session名 USERNAME = "UserName"; ///多少分钟不活动的用户从在线列表中删除 OFFLINEDIFF = 5; ///多少秒执行一次删除不活动用户 REMOVEDIFF = 30; if(System.Web.HttpContext.Current.Application["OnlineTalbe"] == null) { this.CashTableInit(); } } public void CheckOnline() { //从Application获取数据表、获取SessionID DataTable dtOnline; dtOnline = (DataTable)System.Web.HttpContext.Current.Application["OnlineTalbe"]; string sessionId = System.Web.HttpContext.Current.Session.SessionID.ToString(); //数据表中是否有我的记录 DataRow drFind = dtOnline.Rows.Find(sessionId); if(drFind != null) { //有;更新我的状态 Info.Write("更新"); drFind["LastActiveTime"] = DateTime.Now; drFind["UserWhere"] = this.AtWhere; //用户由访客状态变为了登陆会员、或反之 if(Extend.GetSession(USERNAME) != null) { drFind["VisitorName"] = Extend.GetSession(USERNAME); drFind["VisitorLevel"] = Extend.GetSession("UserLevel"); } else { drFind["VisitorName"] = "vst"; drFind["VisitorLevel"] = -1; } } else { //无;加入关于我的在线信息 Info.Write("插入"); DataRow drNew = dtOnline.NewRow(); drNew["SessionID"] = sessionId; drNew["Visit

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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