我有一个 DataTable,其中的 participantAccounts
并未连接到任何数据库或其他东西。
我该如何从中删除一行?
这种方式不起作用:
for (int i = participantAccounts.Rows.Count - 1; i >= 0; i--) {
DataRow dr= participantAccounts.Rows[i];
if (Equals(dr["sellrMembId"].ToString(), itemId))
participantAccounts.Rows.RemoveAt(i);
}
participantAccounts.AcceptChanges();
看起来好像一切都正常,但行仍然留在数据表中。
我也尝试了 dr.Delete()
,但它也不起作用。
当我尝试 participantAccounts.Rows.Remove(dr)
时,我会得到一个异常 The given DataRow is not in the current DataRowCollection.
我做错了什么?
DataRow dr = participantAccounts.Rows[i]; if (Equals(dr["sellrMembId"].ToString(), itemId)) { dr.Delete(); }
- Michaelfor each
并使用.Remove([Datarow])
来删除行。 - FranckRemove
时,我得到了一个异常The given DataRow is not in the current DataRowCollection. (I used it outside of cycle)
。 - MaartyparticipantAccounts.AcceptChanges();
? - MichaelmyDatatable = myDatatable.Select("Price < 100");
- Franck