http://www.crifan.com/csharp_combobox_data_dynamic_binding/
In C #, there is already a list that wants to be dynamic and bound to a ComboBox.
"Resolution Process"
1. Reference:
C # WinForm ComboBox dynamic binding Data
To try the code:
Cmbsubcategory.datasource = generalcategorylist;
Cmbsubcategory.displaymember = "name";
cmbsubcategory.valuemember = "url";
The result is executable, but when you click on the ComboBox, the down menu is empty.
2. Reference:
C # Binding Generic list<string> to Combo Box
Going to try.
3. After the toss, find the reason:
"Understood" code in C # adds a breakpoint, but it cannot be executed when debugging.
4. Then here, in fact, the previous notation is normal:
public struct Categoryitem
{
public string Name {get; set;}
public string Url {get; set;}
};
List<categoryitem> generalcategorylist;
private void Initsearchcategory ()
{
Generalcategorylist = new list<categoryitem> ();
foreach (Htmlnode singleoptionnode in optionnodelist)
{
......
Store Info
Categoryitem Singlecategoryitem = new Categoryitem ();
Singlecategoryitem.name = generalcategory;
Singlecategoryitem.url = Singlecategoryurl;
Add to List
Generalcategorylist.add (Singlecategoryitem);
}
if (Generalcategorylist.count > 0)
{
Init search Category
Cmbsubcategory.datasource = generalcategorylist;
Cmbsubcategory.displaymember = "name";
cmbsubcategory.valuemember = "url";
}
}
You can then see the corresponding options in the ComboBox:
Summary
The binding action, in fact, is simple:
Cmbsubcategory.datasource = generalcategorylist;
Cmbsubcategory.displaymember = "name";
cmbsubcategory.valuemember = "url";
You can do it.
But be aware that:
Here, if you change to:
public struct Categoryitem
{
public string name;
public string URL;
};
List<categoryitem> generalcategorylist;
private void Initsearchcategory ()
{
Generalcategorylist = new list<categoryitem> ();
foreach (Htmlnode singleoptionnode in optionnodelist)
{
......
Store Info
Singlecategoryitem.name = generalcategory;
Singlecategoryitem.url = Singlecategoryurl;
Add to List
Generalcategorylist.add (Singlecategoryitem);
}
if (Generalcategorylist.count > 0)
{
Init search Category
Cmbsubcategory.datasource = generalcategorylist;
Cmbsubcategory.displaymember = "name";
cmbsubcategory.valuemember = "url";
}
}
Then the result is wrong, and then each item is the name of the corresponding object:
The specific root cause, temporarily lazy to delve into.
ComboBox Dynamic Binding Assignment in C #