在c#中保持DataGridView最新的最佳方式

时间:2021-02-24 20:09:09

I want to add a filter textbox to an application so that when a user types text into it, as they type it, the DataGridView will automatically trim down what's being viewed.

我想在应用程序中添加一个过滤器文本框,以便当用户在输入文本时,DataGridView会自动删除正在查看的内容。

Right now DataGridView is bound to a List with my User Objects.

现在,DataGridView与我的用户对象绑定到一个列表。

I was in the process of just using LINQ to create a separate List of User objects and then just re update the DataGridView by doing the whole dataGridView1.DataSource = filteredList but is that the best way to go about this? I feel like I'm doing it the bad way.

我正在使用LINQ创建一个单独的用户对象列表,然后通过执行整个dataGridView1重新更新DataGridView。DataSource = filteredList但这是最好的方法吗?我觉得我这样做很不好。

2 个解决方案

#1


0  

That's probably similar to what I'd go with. Alternatively you could iterate the list and remove any objects no longer matching which will probably be much more memory-friendly.

这可能和我的想法很相似。或者,您可以迭代列表并删除不再匹配的任何对象,这可能更有利于内存。

#2


1  

Your "filteredList" should be a bindingList just so you create a direct interaction between the list and the grid (modifying the grid will modify the list without you having to do any more step).

您的“filteredList”应该是一个bindingList,这样您就可以在列表和网格之间创建一个直接的交互(修改网格将修改列表,而不需要做更多的步骤)。

#1


0  

That's probably similar to what I'd go with. Alternatively you could iterate the list and remove any objects no longer matching which will probably be much more memory-friendly.

这可能和我的想法很相似。或者,您可以迭代列表并删除不再匹配的任何对象,这可能更有利于内存。

#2


1  

Your "filteredList" should be a bindingList just so you create a direct interaction between the list and the grid (modifying the grid will modify the list without you having to do any more step).

您的“filteredList”应该是一个bindingList,这样您就可以在列表和网格之间创建一个直接的交互(修改网格将修改列表,而不需要做更多的步骤)。