简体   繁体   English

DataTable行删除错误

[英]Error in DataTable row deletion

i have a datatable say DT having 11rows and i tried to delete rows having id=3 using 我有一个数据表说DT有11个行,并且我尝试使用以下命令删除id = 3的行

string DeptID ="3";
string s = "id='" + DeptID + "'";
rows = DT.Select(s);
foreach (DataRow r in rows)
r.Delete();

i am having 5 rows having id=3 but after deletion the number of rows in datatable not changed and some red color symbol comes in each fields(columns) of the deleted rows.Can anyone know why this is happening? 我有5行id = 3,但是删除后数据表中的行数没有改变,被删除的行的每个字段(列)都出现了红色符号。有人知道为什么会这样吗?

and whenever i tried to acces the remaining rows using 每当我尝试使用以下方式访问其余行时

     for(int i=0;i<dt1.Rows.Count;i++)
      {
      if (dt1.Rows[i][0].ToString() == "")
        {
        }
      }

it shows error 'deleted rows cannot take' 它显示错误“删除的行不能使用”

After deleting the rows,please give Acceptchanges() to the datatable,so that,it will get synchronized with new data. 删除行后,请为​​数据表提供Acceptchanges(),这样它将与新数据同步。

dt.AcceptChanges();
//And u can also add a condition,while fetching the data,
foreach (DataRow dr in rows)
{  if(dr.RowState!=DataRowState.Deleted ||dr.RowState!=DataRowState.Detached)
{
}
}

You marked the row for deletion, that's why the red color symbols appear. 您将行标记为删除,这就是为什么出现红色符号的原因。 row.Delete() only changes your row state to Deleted. row.Delete()仅将行状态更改为“已删除”。 When you accept then changes (via AcceptChanges method), the actual deletion will occur. 当您接受然后更改(通过AcceptChanges方法)时,将发生实际的删除。

For more information, these are some msdn links: http://msdn.microsoft.com/en-us/library/03c7a3zb.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx 有关更多信息,这些是一些msdn链接: http : //msdn.microsoft.com/en-us/library/03c7a3zb.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow .delete.aspx http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM