Skip to content

Commit 85a2fee

Browse files
authored
Update README.md
1 parent 506b8ab commit 85a2fee

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

README.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
# How to Select Items Programmatically in SfComboBox
2-
This repository provides a detailed example of how to select items programmatically in the WinForms SfComboBox control, which is part of the Syncfusion suite. The SfComboBox control allows users to select multiple items from a dropdown list, and in certain scenarios, you may need to select items dynamically through code rather than user interaction. This is particularly useful when implementing features like loading saved preferences, applying default selections, or responding to specific application logic.
2+
This example demonstrates how to select items programmatically in the Syncfusion WinForms SfComboBox control. The SfComboBox supports multi-selection, making it useful for scenarios where you need to select items dynamically through code rather than user interaction. This is particularly helpful for:
3+
- Loading saved preferences.
4+
- Applying default selections.
5+
- Responding to application logic.
36

4-
The items can be selected programmatically using the CheckedItems property of the DropDownListView. By iterating through the items collection, you can add items to the checked list, ensuring they appear selected in the dropdown. This approach provides flexibility and control over the selection process without requiring manual input.
7+
## Why This Is Useful
8+
- **Dynamic Selection**: Automatically select items based on conditions.
9+
- **Improved UX**: Pre-load user preferences or defaults.
10+
- **Flexibility**: Control selection without manual input.
511

6-
## Code Example (C#)
7-
```csharp
8-
foreach (var selectedItems in this.sfComboBox1.DropDownListView.View.Items.ToList())
12+
## Key Approach
13+
Use the CheckedItems property of the DropDownListView to programmatically select items. By iterating through the items collection, you can add items to the checked list, ensuring they appear selected in the dropdown.
14+
15+
## Code Example
16+
17+
```C#
18+
private void InitializeComponent()
19+
{
20+
this.sfComboBox1 = new Syncfusion.WinForms.ListView.SfComboBox();
21+
((System.ComponentModel.ISupportInitialize)(this.sfComboBox1)).BeginInit();
22+
this.SuspendLayout();
23+
24+
// sfComboBox1
25+
this.sfComboBox1.ComboBoxMode = Syncfusion.WinForms.ListView.Enums.ComboBoxMode.MultiSelection;
26+
this.sfComboBox1.Location = new System.Drawing.Point(167, 76);
27+
this.sfComboBox1.Name = "sfComboBox1";
28+
this.sfComboBox1.Size = new System.Drawing.Size(171, 37);
29+
this.sfComboBox1.TabIndex = 0;
30+
31+
// Form1
32+
this.ClientSize = new System.Drawing.Size(535, 294);
33+
this.Controls.Add(this.sfComboBox1);
34+
this.StartPosition = FormStartPosition.CenterScreen;
35+
this.Text = "SfComboBox_Selection";
36+
37+
((System.ComponentModel.ISupportInitialize)(this.sfComboBox1)).EndInit();
38+
this.ResumeLayout(false);
39+
}
40+
41+
public Form1()
942
{
10-
// Programmatically add the checked items
11-
this.sfComboBox1.DropDownListView.CheckedItems.Add(selectedItems);
43+
// Bind data source
44+
sfComboBox1.DataSource = GetTable();
45+
sfComboBox1.DisplayMember = "Patient";
46+
sfComboBox1.ValueMember = "Drug";
47+
48+
// Programmatically select all items
49+
foreach (var item in sfComboBox1.DropDownListView.View.Items.ToList())
50+
{
51+
sfComboBox1.DropDownListView.CheckedItems.Add(item);
52+
}
53+
}
54+
55+
private DataTable GetTable()
56+
{
57+
DataTable table = new DataTable();
58+
table.Columns.Add("Dosage", typeof(int));
59+
table.Columns.Add("Drug", typeof(string));
60+
table.Columns.Add("Patient", typeof(string));
61+
62+
table.Rows.Add(25, "Indocin", "David");
63+
table.Rows.Add(50, "Enebrel", "Sam");
64+
table.Rows.Add(10, "Hydralazine", "Christoff");
65+
table.Rows.Add(21, "Combivent", "Janet");
66+
table.Rows.Add(100, "Dilantin", "Melanie");
67+
68+
return table;
1269
}
1370
```
1471

1572
## Reference
16-
For more details, refer to the official Syncfusion Knowledge Base article:
17-
[How to select DropDown items in SfComboBox](https://wwwnforms-sfcombobox-dropdown-items-programmatically)
73+
For more details, refer to the official Syncfusion Knowledge Base article: [How to select DropDown items in SfComboBox](https://wwwnforms-sfcombobox-dropdown-items-programmatically)

0 commit comments

Comments
 (0)