Datagridviews 中的 VLookup (VB.NET)

发布于 2024-08-06 09:52:53 字数 446 浏览 3 评论 0原文

我有 2 个 datagridview(例如,DGV-A 和 DGV-B),每个数据网格视图只有一列。 我想查找DGV-A中的项目是否也存在于DGV-B中。基本上,我正在寻找 MS-Excel 中可用的 VLookup 函数。 这可以简单地完成,总线迭代 DGV-A 中的值,并且对于 DGV-A 的每次迭代,迭代 DGV-B 并查看它是否存在(一旦我们找到该项目,就中止 DGV-B 的迭代)存在)。 需要对 DGV-A 中的所有项目执行此操作。而且因为我的 DGV 可能在每个 datagridviews 中包含大约 200 个项目(例如,如果每个 DGV 包含 200 个项目,在最坏的情况下,我会进行 200*200 = 40000 次比较),所以我担心,这不会很快。

有没有什么方法/算法可以以最佳方式做到这一点。 (我没有任何数据绑定或数据库,因此不能选择使用 SQL/DB-Engine;我在 DGV 中的数据是根据用户操作以编程方式动态生成的)

I have 2 datagridviews (say, DGV-A and DGV-B) having just one column each.
I want to find whether the item in DGV-A also exists in DGV-B. Basically, I am looking for a VLookup function available in MS-Excel.
This can be done trivially, bus iterating over the values in DGV-A and for each iteration of DGV-A, iterate over DGV-B and see if it exists there (aborting iteration of DGV-B as soon as we have found the item exists).
This needs to be done for all the items in DGV-A. And because my DGV's could potentially have around 200 items (e.g. if DGV contain 200 items each, at worst, I would be doing 200*200 = 40000 comparisions) in each datagridviews, I am afraid, it is not going to be quick.

Is there any way/algorithm to do it in an optimal way. (I do not have any data binding or database, so use of SQL/DB-Engine is not an option; my data in DGV's is generated programmatically on the fly based on the user actions)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-08-13 09:52:53

在您知道确实存在性能问题之前,优化代码以提高性能通常不是一个好主意。但在这种情况下,我也会使用另一种解决方案来避免 O(n * m) 操作。

我建议将一个列表中的所有项目插入到哈希集中 - 如果您指定足够大的初始大小并避免以这种方式调整哈希集的大小,则这将是 O(n)。然后只需在 O(m) 中对第二个列表中的每个项目在哈希集中执行查找即可。这样你就可以将 O(n * m) 降低到 O(n + m)

It is usually not a good idea to optimize code for performance until you know that you really have performance problems. But in this case I would use another solution, too, to avoid an O(n * m) operation.

I suggest to insert all items from one list into a hash set - this will be O(n) if you specify a large enough initial size and avoid resizing the hash set this way. Then just perform a look up in the hash set for each item in the second list in O(m). This way you get O(n * m) down to O(n + m).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文