The ComboBox and ComboBoxEdit controls developed by Winform bind key/value data and comboboxkeyvalue

Source: Internet
Author: User

The ComboBox and ComboBoxEdit controls developed by Winform bind key/value data and comboboxkeyvalue

Bind the key/value using the ComboBox control:

Because ComboBox has the DataSource attribute, it can directly bind data sources, such as DataTable and ListItem.

Use DataTable to bind directly:

Public void BindSource () {DataTable dt = new DataTable (); dt. columns. add ("Text", Type. getType ("System. string "); dt. columns. add ("Value", Type. getType ("System. string "); dt. rows. add ("select", "0"); dt. rows. add ("option 1", "1"); dt. rows. add ("option 2", "2"); dt. rows. add ("option 3", "3"); comboBox1.DataSource = dt; comboBox1.DisplayMember = "Text"; // Text, that is, the explicit Text comboBox1.ValueMember = "Value "; // Value, that is, the actual Value comboBox1.SelectedIndex = 0; // set it to the first by default}
String text = this. comboBox1.Text; // obtain the selected text string value = this. comboBox1.SelectedValue. ToString (); // obtain the selected value

Use ListItem to implement key/value:

    public class ListItem : Object    {        public string Text { get; set; }        public string Value { get; set; }        public ListItem(string text,string value)        {            this.Text = text;            this.Value = value;        }        public override string ToString()        {            return this.Text;        }    }
Public void BindSource () {List <ListItem> list = new List <ListItem> (); list. add (new ListItem ("select", "0"); list. add (new ListItem ("option 1", "1"); list. add (new ListItem ("option 2", "2"); list. add (new ListItem ("option 3", "3"); comboBox1.DisplayMember = "Text"; // Text, that is, the explicit Text comboBox1.ValueMember = "Value "; // Value, that is, the actual Value comboBox1.DataSource = list; comboBox1.SelectedValue = "0"; // you can specify a Value of 0}
String text = (this. comboBox1.SelectedItem as ListItem). Text; // obtain the selected item text string value = (this. comboBox1.SelectedItem as ListItem). Value; // obtain the value of the selected item

 

Bind the key/value using the ComboBoxEdit control:

Because ComboBoxEdit does not have the DataSource attribute, you cannot bind the data source directly. You can add only one item.

    public class ListItem : Object    {        public string Text { get; set; }        public string Value { get; set; }        public ListItem(string text,string value)        {            this.Text = text;            this.Value = value;        }        public override string ToString()        {            return this.Text;        }    }
Public void BindSource () {string text = string. empty; string value = string. empty; ListItem item = null; for (int I = 0; I <4; I ++) {if (I = 0) {text = "select ";} else {text = "option" + I. toString ();} value = I. toString (); item = new ListItem (text, value); this. comboBoxEdit1.Properties. items. add (item );}}

When obtaining the value of the selected item, be sure to determine whether to select it.

String text = string. empty; string value = string. empty; if (comboBoxEdit1.SelectedIndex <0) // if it is less than 0, it indicates that it is not selected. if it is input, it is smaller than 0 {text = comboBoxEdit1.Text. trim (); // only the input text can be obtained} else {text = (comboBoxEdit1.SelectedItem as ListItem ). text; // obtain the selected Text value = (comboBoxEdit1.SelectedItem as ListItem ). value; // obtain the Value of the selected item}

 

Related Article

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.