【发布时间】:2023-04-08 01:43:01
【问题描述】:
场景
我有一个带有 gridview 的 c# winforms 应用程序。 gridview 数据源由数据集填充。 该数据集的内容每隔几秒钟就会更新一次,并且数据集中的项目数量并不总是相同等。 数据集实际上是通过一次传入多个其他数据集来填充(或需要填充)的。这些数据集在完全随机的时间更新,这是无法控制的。
问题
由于本质上 GridView 数据集正在“持续更新” - 在更新时(其中包含最新信息的新数据集需要与现有 GridView 数据源合并),我需要采取填充 gridview 的当前数据集的副本,将其与来自其他数据集的传入数据进行比较,并仅更新新数据。
到目前为止
我尝试了一系列不同的方法,但我似乎无法让它发挥作用。我要么最终将数据集不断地添加到具有略微不同数据的副本中,要么随机删除行(我尝试使用仅偶尔有效的合并全部然后删除的方法)。
有什么办法可以解决这个问题!?
目前,gridview 只是更新自身以显示最新传入的数据集,而没有将先前的数据集合并到其中.....
代码 - 这会随机删除行,即使它在某些时候会显示所有数据
//If the existing GridView dataset contains any rows that
//exist in the incoming latest dataset (matched on the two unique fields
//of the dataset), then remove that row from the existing GridView dataset
for (int i = 0; i < existingPriceResultsDTCopyForCompare.Rows.Count; i++)
{
foreach (DataRow incomingRow in incomingDS.Tables[0].Rows)
{
string incomingDate = incomingRow["Date"].ToString();
DataRow currentRow = existingPriceResultsDTCopyForCompare.Rows[i];
if ((currentRow["CCY"].ToString().Contains(incomingCcy))
&& (currentRow["Date"].ToString().Contains(incomingDate)))
{
existingPriceResultsDT.Rows.RemoveAt(i);
}
}
}
//Then merge the existing GridView dataset (Minus the old data that
//matches the data from the incoming Dataset with the latest information),
//With the brand new incoming data.
incomingDS.Merge(existingPriceResultsDT);
编辑-
我开始怀疑传入的数据集是否会在迭代有时间完成之前一直被下一个传入的数据集覆盖。所以我猜我需要锁定传入的数据集?
【问题讨论】:
-
我们是否可以假设上述代码示例中指向的所有数据集在您迭代它们时都是稳定的?