Use of the ComboBox control _ binding, adding options, and clearing

Source: Internet
Author: User

Bind arraylist (namespace system. Collections)

View plaincopy to clipboardprint?

    1. Arraylist al1 = new arraylist ();
    2. Al1.add (New dictionaryentry ("Y", "Military Product "));
    3. Al1.add (New dictionaryentry ("N", "minipin "));
    4. Cbisarmy. datasource = al1;
    5. Cbisarmy. displaymember = "value ";
    6. Cbisarmy. valuemember = "key ";

Arraylist al1 = new arraylist (); al1.add (New dictionaryentry ("Y", ""); al1.add (New dictionaryentry ("N", ""); cbisarmy. datasource = al1; cbisarmy. displaymember = "value"; cbisarmy. valuemember = "key ";

The ComboBox bound with datesource cannot use the items. Add method recently used the ComboBox control. To briefly summarize its usage (bind, add options, and clear ):

I. ComboBox binding

View plaincopy to clipboardprint?

    1. Oledb = new oledb ();
    2. Private void form1_load (Object sender, eventargs E)
    3. {
    4. Datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable
    5. Combobox1.datasource = DT;
    6. Combobox1.displaymember = "name"; // display content
    7. Combobox1.valuemember = "ID"; // value corresponding to the option
    8. }

Oledb = new oledb (); Private void form1_load (Object sender, eventargs e) {datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable combobox1.datasource = DT; combobox1.displaymember = "name"; // The displayed content combobox1.valuember = "ID "; // value corresponding to the option}

2. Add options for ComboBox

The ComboBox bound with datesource cannot use items. the add method adds options, which can only be implemented by modifying the able. For example, to add the option "--- all ---" to the combox1 implemented above, you can do this:

View plaincopy to clipboardprint?

    1. Private void form1_load (Object sender, eventargs E)
    2. {
    3. Datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable
    4. Datarow DR = DT. newrow ();
    5. Dr ["ID"] = 0;
    6. Dr ["name"] = "--- all ---";
    7. DT. rows. insertat (DR, 0); // use DT. rows. add (DR) only adds the options to the end, and uses DT. rows. insertat (DR, 0) can insert the added options to the corresponding positions.
    8. Combobox1.datasource = DT;
    9. Combobox1.displaymember = "name"; // display content
    10. Combobox1.valuemember = "ID"; // value corresponding to the option
    11. }

Private void form1_load (Object sender, eventargs e) {datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable datarow DR = DT. newrow (); Dr ["ID"] = 0; Dr ["name"] = "--- all ---"; DT. rows. insertat (DR, 0); // use DT. rows. add (DR) only adds the options to the end, and uses DT. rows. insertat (DR, 0) can insert the added options to the corresponding position combobox1.datasource = DT; combobox1.displaymember = "name"; // The displayed content combobox1.valuemember = "ID "; // value corresponding to the option}

3. Clear the options in ComboBox

If the options in ComboBox are handwritten, use combobox1.items. clear (); method to achieve this, and the ComboBox bound to datasource cannot use this method. In fact, in this case, clearing the option is also very simple. Use combobox1.datasource = NULL; you can solve the problem.

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.