C# 如何在 DataGridView 中显示 ConcurrentDictionary (WinForms)
我有一个对象“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CurrentDictionary
不直接支持UI中的显示,因为它不是iList
或ilistsource
。因此,您将需要创建一个代表实现这些接口之一的字典内容的对象,并将其分配给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 anIList
or anIListSource
. 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 theDataGridView.DataSource
, and update that list from the UI thread.One possible approach for this is to add a
Timer
to yourForm
that fires periodically (say once a second or so), and compares the contents of your dictionary to the contents of theDataGridView.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 aBindingList<T>
.