C# 如何在 DataGridView 中显示 ConcurrentDictionary (WinForms)

发布于 2025-01-18 22:20:27 字数 1109 浏览 2 评论 0原文

我有一个对象“User”

public class User
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string LastRequestId { get; set; }
    }

在主程序中我有一个ConcurrentDictionary“UserList”(由多个线程读/写)

private ConcurrentDictionary<string, User> UserList;

有没有办法在DataGridView中显示ConcurrentDictionary?

-------------------------------------------------------
| Username | First Name | Last Name | Last Request Id |
-------------------------------------------------------
| User_1   | John       | Smith     | REQ_000123      |
-------------------------------------------------------
| User_2   | Jane       | Doe       | REQ_000124      |
-------------------------------------------------------
| User_3   | Will       | Iam       | REQ_000125      |
-------------------------------------------------------
| User_4   | Robert     | New       | REQ_000126      |
-------------------------------------------------------

列表应显示实时数据

I have an object "User"

public class User
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string LastRequestId { get; set; }
    }

In the main program I have a ConcurrentDictionary "UserList" (read/write by multiple threads)

private ConcurrentDictionary<string, User> UserList;

Is there a way to display the ConcurrentDictionary in a DataGridView?

-------------------------------------------------------
| Username | First Name | Last Name | Last Request Id |
-------------------------------------------------------
| User_1   | John       | Smith     | REQ_000123      |
-------------------------------------------------------
| User_2   | Jane       | Doe       | REQ_000124      |
-------------------------------------------------------
| User_3   | Will       | Iam       | REQ_000125      |
-------------------------------------------------------
| User_4   | Robert     | New       | REQ_000126      |
-------------------------------------------------------

The list should display real time data

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

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

发布评论

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

评论(1

红颜悴 2025-01-25 22:20:27

CurrentDictionary不直接支持UI中的显示,因为它不是iListilistsource。因此,您将需要创建一个代表实现这些接口之一的字典内容的对象,并将其分配给datagridview.datasource,然后从UI线程更新该列表。

一种可能的方法是将计时器添加到您的表单定期触发的表单(例如,一秒钟左右),并将您的字典的内容与内容进行比较datagridview.datasource并同步它们。 (去除已删除的元素,添加已添加的元素等)。如果您很快添加了很多条目,则这种方法根本无法扩展,但这似乎不太可能。

这是使用常规字典&lt; tkey,tvalue&gt; datagridview bongo字典,描述了将字典类型转换为bindingList&lt; t&gt;的几种方法。

CurrentDictionary doesn't support display directly in a UI because it is not an IList or an IListSource. So you will need to create an object that is representative of your Dictionary's contents that implements one of those interfaces, assign it to the DataGridView.DataSource, and update that list from the UI thread.

One possible approach for this is to add a Timer to your Form that fires periodically (say once a second or so), and compares the contents of your dictionary to the contents of the DataGridView.DataSource and synchronizes them. (Removes elements that have been removed, adds elements that have been added, etc). This approach won't scale well at all if you have lots of entries being added very quickly, but that seems unlikely.

Here's an example using a regular Dictionary<TKey,TValue> DataGridView bound to a Dictionary, which describes several ways to convert a dictionary type to a BindingList<T>.

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