Bind both encoding and name to ComboBox control (WinForm)

Source: Internet
Author: User
ComboBox Control | Encoding when using ComboBox, it is often necessary to display to the user name, but to receive user information, but to receive the user's choice of encoding, the following method to bind both the encoding and the name to the ComboBox control. You can always remove the name of the item selected by the user or the project code selected.

Generate a new DataTable (or it can be a result of a query from a database)
DataTable dt = new DataTable ();
Dt. Columns.Add ("id");
Dt. Columns.Add ("name");

Adding data to a DataTable
DataRow dr = dt. NewRow ();
dr["id"] = "11";
dr["name"] = "AAAAA";
Dt. Rows.Add (DR);
Dr = dt. NewRow ();
dr["id"] = "22";
dr["name"] = "bbbbb";
Dt. Rows.Add (DR);

Binding display content and actual values
Combobox1.valuemember = "id";
Combobox1.displaymember = "name";
Binding Data
Combobox1.datasource = DT;

At last
Take value Combobox1.text
Take the display content Combobox1.selectedvalue




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.