Filtering Listboxes in a Windows Forms application

无人久伴 提交于 2019-12-24 20:06:38

问题


Is it possible to filter the contents of a Listbox in a Windows Forms application?

The DataSource of my ListBox is a BindingSource containing a bunch of DTOs in an:

IList<DisplayDTO>

I want to filter on the DTO property that is specified in the ListBox's DisplayMember.

The text to be filtered on is provided in a separate Text Box.


回答1:


This should work :

private void textBox_TextChanged(object sender, EventArgs e)
{
    bindingSource.Filter = string.Format("[{0}] LIKE '%{1}%'",
                                         listBox.DisplayMember,
                                         textBox.Text.Replace("'", "''"));
}

EDIT: this works only if the underlying data source (bindingSource.DataSource) implements IBindingListView. In the FCL, only the DataView class implements this interface.

You can create your own implementation by inheriting from BindingList<T>. Here's an article that explains how to add the filter functionality. You can also find various implementations of SortableBindingList on Google.



来源:https://stackoverflow.com/questions/1419930/filtering-listboxes-in-a-windows-forms-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!