【发布时间】:2020-04-18 23:19:15
【问题描述】:
我有 1 个具有 10 行的数据表,而具有 8 个列表项的列表框包含来自数据表的 6 条记录和 2 条新记录。
我想更新 DataTable,使 6 条记录保持原样,并从 DataTable 中删除剩余的 4 条记录,并从 DataTable 中的 ListBox 中添加 2 个新添加的条目。
我尝试的是从 DataTable 循环 ListBox 记录并创建匹配记录列表。
string impactedTC;
List<int> index = new List<int>();
// This retruns my dataset having 10 records
DataTable dttable = GetImpactedTestCaseDetailsToUpdateStatus().Tables[0];
for (int i = 0; i < ListBox1.Items.Count; i++)
{
int count = 0;
string dTestCase = ListBox1.Items[i].Text;
foreach (DataRow dtRow in dttable.Rows)
{
impactedTC = dtRow["TestCaseName"].ToString();
if (impactedTC == dTestCase)
{
index.Add(count);
}
count++;
}
}
【问题讨论】:
-
请提供示例代码,以便我们可以使用。
-
@djsoteric 我创建了与数据表记录匹配的索引列表。现在我想从数据表中删除其余的记录。
-
您应该能够遍历记录并在您确定不属于的行之后在一个事务中删除它们。我会用 LINQ 来做,而不是写一个循环。见stackoverflow.com/questions/5648339/…
-
@djsoteric 你能用你的逻辑更新上面的代码吗?
-
您想将语句 count++ 放在 if 语句中,这样它只会在您匹配 TestCaseName 时递增。
标签: c# .net datatable web-controls