首页 > 解决方案 > 用于 DataGridView (Windows.Forms) 和过滤的数组、列表或数据表

问题描述

我正在从几个源(csv 文件,..)数据读取到数组,然后通过 foreach() { .Add(..) } 读取到 DatagridView。

现在我希望有可能通过用户的文本字段输入来搜索控件中的数据。

GenericList<T>这可能是我在 C# 中制作 DataTable 而不是数组或此类的最佳方式吗?

标签: c#winforms

解决方案


您可以使用数据表

DataTable table = new DataTable();
BindingSource bs = new BindingSource();

使用 BindingSource 绑定数据表

dataGridView1.DataSource = bs;
bs.DataSource = table;

过滤记录

bs.Filter= string.Format("column LIKE '%{0}%'", value);

为什么不列出?

列表未实现IBindingList,因此网格不知道您的新项目。

为什么不绑定列表?

您不能使用Filter属性来过滤BindingSource它的 DataSource 设置为BindingList<T>.

如何使用自定义对象?

您可以创建一个扩展BiningList<>请参阅此)。


推荐阅读