winform中ComboBox实现text和value,使显示和值分开,重写text和value属性

winform的ComboBox中只能赋值text,显示和值是一样的,很多时候不能满足根本需要,熟悉B/S开发的coder最常用的就是text和value分开的,而且web下DropDownList本来就是分为text和value。ComboBox要实现同样功能,使item有多个值,只能用重写一个类来实现了。

重写类如下:

using System;

namespace sm
{
    class cListItem
    {
        private string id = string.Empty;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        private string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public cListItem(string name, string id)
        {
            this.id = id;
            this.name = name;
        }

        public override string ToString()
        {
            return this.name;
        }

    }
}

  绑定数据时:

bubufxComboBox.Items.Add(new cListItem(drv["bubufx_text"].ToString(), drv["ID"].ToString()));

  取值时:

string bubufxComboBox_str = ((cListItem)bubufxComboBox.SelectedItem).ID;

  bubufx分享,禁止转载。原文:【winform中ComboBox实现text和value,使显示和值分开,重写text和value属性】

原文地址:https://www.cnblogs.com/weekzero/p/3483196.html