我有participantAccounts未连接到任何数据库或其他东西的DataTable 。
如何从中删除一行?
这不起作用:
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();
Run Code Online (Sandbox Code Playgroud)
它就像一切都很好,但行仍然保留在 DataTable 中。我也试过,dr.Delete()但都不行。当我尝试时participantAccounts.Rows.Remove(dr)出现异常The given DataRow is not in the current DataRowCollection.
我做错了什么?
最后我找到了解决方案。IT 与删除行的方式无关。
DataTable dt = participantAccounts;
dt.Rows.Remove(dt.Rows.Find(itemId));
participantAccounts = dt;
Run Code Online (Sandbox Code Playgroud)
我认为问题是,participantAccounts 是一个视图状态(.ASP),因此它没有用直接方法更新。
谢谢大家的帮助