|
1 | 1 | # How to Filter the Dropdown Items in WinForms SfComboBox |
2 | | -This repository demonstrates how to filter items in the Syncfusion WinForms SfComboBox control using the built-in filtering feature. The SfComboBox is a modern replacement for the standard ComboBox, offering advanced features such as data binding, auto-complete, and customizable filtering logic. Filtering allows you to display only relevant items based on user-defined conditions, improving usability and performance in applications with large datasets. |
| 2 | +This repository demonstrates how to filter items in the Syncfusion WinForms **SfComboBox** control using its built-in filtering feature. The SfComboBox is a modern replacement for the standard ComboBox, offering advanced features such as **data binding**, **auto-complete**, and **customizable filtering** logic. Filtering allows you to display only relevant items based on user-defined conditions, improving usability and performance in applications with large datasets. |
3 | 3 |
|
4 | 4 | ## Key Features Demonstrated in This Sample |
5 | | - • Apply custom filtering logic using the Filter property of DropDownListView. |
6 | | - • Refresh the filter dynamically at runtime. |
7 | | - • Use predicates to determine which items should be visible in the dropdown. |
| 5 | +- Apply custom filtering logic using the Filter property of DropDownListView. |
| 6 | +- Refresh the filter dynamically at runtime. |
| 7 | +- Use predicates to determine which items should be visible in the dropdown. |
8 | 8 |
|
9 | 9 | ## How Filtering Works |
10 | | -The Filter property accepts a predicate that is evaluated for each item in the data source. If the predicate returns true, the item remains visible; otherwise, it is hidden. This approach provides flexibility to implement complex filtering scenarios such as prefix matching, substring search, or conditional visibility based on multiple fields. |
| 10 | +The _Filter_ property accepts a **predicate** that is evaluated for each item in the data source. |
| 11 | +- If the predicate returns _true_, the item remains visible. |
| 12 | +- If it returns _false_, the item is hidden. |
| 13 | + |
| 14 | +This approach provides flexibility to implement complex filtering scenarios such as: |
| 15 | +- Prefix matching |
| 16 | +- Substring search |
| 17 | +- Conditional visibility based on multiple fields |
11 | 18 |
|
12 | 19 | ## Example Code (C#) |
13 | | -```csharp |
| 20 | +```C# |
14 | 21 | public Form1() |
15 | 22 | { |
16 | | - InitializeComponent(); |
| 23 | + InitializeComponent(); |
17 | 24 |
|
18 | | - List<USState> list = GetData(); |
19 | | - this.sfComboBox1.DataSource = list; |
20 | | - this.sfComboBox1.DisplayMember = "LongName"; |
21 | | - this.sfComboBox1.ThemeName = "Office2016Colorful"; |
22 | | - this.sfComboBox1.DropDownListView.Style.ItemStyle.Font = new Font("Microsoft Sans Serif", 9.75f); |
| 25 | + List<USState> list = GetData(); |
| 26 | + this.sfComboBox1.DataSource = list; |
| 27 | + this.sfComboBox1.DisplayMember = "LongName"; |
| 28 | + this.sfComboBox1.ThemeName = "Office2016Colorful"; |
| 29 | + this.sfComboBox1.DropDownListView.Style.ItemStyle.Font = new Font("Microsoft Sans Serif", 9.75f); |
23 | 30 |
|
24 | | - // Apply filter |
25 | | - sfComboBox1.DropDownListView.View.Filter = FilterItem; |
26 | | - sfComboBox1.DropDownListView.View.RefreshFilter(); |
| 31 | + // Apply filter |
| 32 | + sfComboBox1.DropDownListView.View.Filter = FilterItem; |
| 33 | + sfComboBox1.DropDownListView.View.RefreshFilter(); |
27 | 34 | } |
28 | 35 |
|
29 | 36 | // Filter predicate |
30 | 37 | private bool FilterItem(object data) |
31 | 38 | { |
32 | | - if ((data as USState).LongName.StartsWith("C")) |
33 | | - return true; |
34 | | - return false; |
| 39 | + return (data as USState).LongName.StartsWith("C"); |
35 | 40 | } |
36 | 41 | ``` |
| 42 | +This sample filters the dropdown items so that only states starting with the letter "C" are displayed. You can modify the predicate to implement custom logic such as filtering by length, region, or any other property. |
37 | 43 |
|
38 | | -This sample filters the dropdown items so that only states starting with the letter C are displayed. You can modify the predicate to implement custom logic such as filtering by length, region, or any other property. |
39 | | - |
40 | | -For more details, refer to the official UG documentation: |
41 | | -🔗 [Filtering in Windows Forms SfComboBox](https://help.syncfusion.com/windowsforms/combobox/filtering) |
| 44 | +## Reference |
| 45 | +For more details, refer to the official UG documentation: [Filtering in Windows Forms SfComboBox](https://help.syncfusion.com/windowsforms/combobox/filtering) |
0 commit comments