【问题标题】:Filtering Listboxes in a Windows Forms application在 Windows 窗体应用程序中过滤列表框
【发布时间】:2009-09-15 07:16:22
【问题描述】:

是否可以在 Windows 窗体应用程序中过滤列表框的内容?

我的 ListBox 的 DataSource 是一个 BindingSource,其中包含一堆 DTO:

IList<DisplayDTO>

我想过滤 ListBox 的 DisplayMember 中指定的 DTO 属性。

要过滤的文本在单独的文本框中提供。

【问题讨论】:

    标签: c# listbox filtering windows-forms-designer


    【解决方案1】:

    这应该可行:

    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&lt;T&gt; 继承来创建自己的实现。这是an article,它解释了如何添加过滤器功能。您还可以在 Google 上找到 SortableBindingList 的各种实现。

    【讨论】:

    • 我已经尝试了您的建议,有一些变化,但仍然无法使其正常工作。我读到这可能与 BindingSource 中使用的容器有关。将其更改为 BindingList 没有任何帮助。
    • 很遗憾,.NET 框架提供的BindingList&lt;T&gt; 实现不支持排序和过滤...这里有一篇解释如何添加过滤功能的帖子:nablasoft.com/alkampfer/index.php/2008/11/22/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2018-10-16
    • 2011-08-24
    相关资源
    最近更新 更多