是否可以在 Windows 窗体应用程序中过滤列表框的内容?
我的 ListBox 的 DataSource 是一个 BindingSource,其中包含一堆 DTO:
IList<DisplayDTO>
我想过滤 ListBox 的 DisplayMember 中指定的 DTO 属性。
要过滤的文本在单独的文本框中提供。
是否可以在 Windows 窗体应用程序中过滤列表框的内容?
我的 ListBox 的 DataSource 是一个 BindingSource,其中包含一堆 DTO:
IList<DisplayDTO>
我想过滤 ListBox 的 DisplayMember 中指定的 DTO 属性。
要过滤的文本在单独的文本框中提供。
这应该工作:
private void textBox_TextChanged(object sender, EventArgs e)
{
bindingSource.Filter = string.Format("[{0}] LIKE '%{1}%'",
listBox.DisplayMember,
textBox.Text.Replace("'", "''"));
}
编辑:这仅在底层数据源(bindingSource.DataSource
)实现时才有效IBindingListView
。在 FCL 中,只有DataView
类实现了这个接口。
您可以通过继承来创建自己的实现BindingList<T>
。这是一篇解释如何添加过滤器功能的文章。您还可以SortableBindingList
在 Google 上找到各种实现。