C # Add "other" items to the winform drop-down list box (ComboBox)

Source: Internet
Author: User

Sometimes, after the results returned from the database are associated with the ComboBox, you also need to add items such as "Others" or "all.

You can create a new listitem on the web, but winform does not.

You can add a datarow in datatable to implement the same function.

String sequence string = "select ID, name from staff"; <br/> try <br/>{< br/> sqldatabase SDB = new sqldatabase (); <br/> SDB. open (); <br/> datatable dtbl = SDB. executedatatable (optional string); <br/> SDB. close (); <br/> // Add the "all" option <br/> datarow DR = dtbl. newrow (); <br/> Dr ["ID"] = dbnull. value; <br/> Dr ["name"] = "all"; <br/> dtbl. rows. insertat (DR, 0); <br/> cbostaff. datasource = dtbl; <br/> bostaff. selectedindexchanged + = new eventhandler (cbostaff_selectedindexchanged); <br/>}< br/> catch <br/>{< br/>}

The staff table in the database contains two fields: ID and name. The result table returned by the query is dtbl,

Dtbl. newrow () generates a new datarow, and then sets the IDs and names of the two Dr fields to dbnull. Value and "all" respectively ".

Dtbl. Rows. insertat () inserts datarow at the beginning of the list. You can also insert it to the end of the list.

Then cbostaff. datasource = dtbl is enough.

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.