Winform開發之ComboBox和ComboBoxEdit控件綁定key/value數據


使用 ComboBox 控件綁定key/value值:

因為 ComboBox 是有 DataSource 屬性的,所以它可以直接綁定數據源,如 DataTable、ListItem 等。

使用 DataTable 直接綁定:

     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("請選擇", "0");
            dt.Rows.Add("選項一", "1");
            dt.Rows.Add("選項二", "2");
            dt.Rows.Add("選項三", "3");

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "Text";   // Text,即顯式的文本
            comboBox1.ValueMember = "Value";    // Value,即實際的值
            comboBox1.SelectedIndex = 0;        //  設置為默認選中第一個
        }
string text = this.comboBox1.Text;      //獲取選中項文本
string value = this.comboBox1.SelectedValue.ToString();     //獲取選中項的值

使用 ListItem 實現 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("請選擇", "0"));
            list.Add(new ListItem("選項一", "1"));
            list.Add(new ListItem("選項二", "2"));
            list.Add(new ListItem("選項三", "3"));

            comboBox1.DisplayMember = "Text";   // Text,即顯式的文本
            comboBox1.ValueMember = "Value";    // Value,即實際的值
            comboBox1.DataSource = list;
            comboBox1.SelectedValue = "0";        //  設置選擇值為 0 的項
        } 
string text = (this.comboBox1.SelectedItem as ListItem).Text;      //獲取選中項文本
string value = (this.comboBox1.SelectedItem as ListItem).Value;     //獲取選中項的值

 

使用 ComboBoxEdit 控件綁定key/value值:

因為 ComboBoxEdit 沒有 DataSource 屬性,所以不能直接綁定數據源,只能一項一項的添加。

    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 = "請選擇";
                }
                else
                {
                    text = "選項" + i.ToString();
                }
                value = i.ToString();

                item = new ListItem(text, value);
                this.comboBoxEdit1.Properties.Items.Add(item);
            }
        }

獲取選中項的值時,注意判斷是否選擇。

string text = string.Empty;
string value = string.Empty;

if (comboBoxEdit1.SelectedIndex < 0)    //小於0,表示未選擇,如果是輸入的也小於0
{
     text = comboBoxEdit1.Text.Trim();     //只能獲取輸入的文本
}
else
{
     text= (comboBoxEdit1.SelectedItem as ListItem).Text;        //獲取選中項文本
     value = (comboBoxEdit1.SelectedItem as ListItem).Value;        //獲取選中項的值
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM