ComboBox Dynamic Binding Assignment in C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.