Winform ComboBox控件数据绑定, 设置与获取数据源的显示值与实际值, DisplayMember, ValueMember

新建Form窗体,拖动ComboBox控件,3个Button按钮控件

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// ComboBoxItem
        /// </summary>
        public class ComboBoxItem
        {
            /// <summary>
            /// 文本
            /// </summary>
            public string Text { get; set; }

            /// <summary>
            /// 值
            /// </summary>
            public int Value { get; set; }

            /// <summary>
            /// 输出
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return Text;
            }
        }

        /// <summary>
        /// 使用方式1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();

            ComboBoxItem item = new ComboBoxItem();
            item.Text = "未知";
            item.Value = 0;
            this.comboBox1.Items.Add(item);

            item = new ComboBoxItem();
            item.Text = "男";
            item.Value = 1;
            this.comboBox1.Items.Add(item);

            item = new ComboBoxItem();
            item.Text = "女";
            item.Value = 2;
            this.comboBox1.Items.Add(item);

            // 选中默认
            comboBox1.SelectedIndex = 1;

            // 获取选中项
            ComboBoxItem comboBoxItem = (ComboBoxItem)comboBox1.SelectedItem;

            // 显示值
            MessageBox.Show("显示值:" + comboBoxItem.Text.ToString());

            // 实际值
            MessageBox.Show("实际值:" + comboBoxItem.Value.ToString());
        }

        /// <summary>
        /// 使用方式2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();

            List<ComboBoxItem> items = new List<ComboBoxItem>();
            ComboBoxItem item = new ComboBoxItem();
            item.Text = "未知";
            item.Value = 0;
            items.Add(item);

            item = new ComboBoxItem();
            item.Text = "男";
            item.Value = 1;
            items.Add(item);

            item = new ComboBoxItem();
            item.Text = "女";
            item.Value = 2;
            items.Add(item);

            // 非必要设置
            comboBox1.DataSource = items;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";

            // 选中默认
            comboBox1.SelectedIndex = 2;

            // 获取选中项
            MessageBox.Show("显示值:" + comboBox1.Text.ToString());
            MessageBox.Show("实际值:" + comboBox1.SelectedValue.ToString());
        }

        /// <summary>
        /// 使用方式3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();

            DataTable dt = new DataTable();
            dt.TableName = "dt";
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");

            DataRow dr = dt.NewRow();
            dr["Text"] = "未知";
            dr["Value"] = "0";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Text"] = "男";
            dr["Value"] = "1";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Text"] = "女";
            dr["Value"] = "2";
            dt.Rows.Add(dr);

            this.comboBox1.DataSource = dt;
            this.comboBox1.DisplayMember = "Text";
            this.comboBox1.ValueMember = "Value";

            // 选中默认
            comboBox1.SelectedIndex = 1;

            // 获取选中项
            MessageBox.Show("显示值:" + comboBox1.Text.ToString());
            MessageBox.Show("实际值:" + comboBox1.SelectedValue.ToString());
        }
    }
推荐采用方式一


作者最新文章
C# 使用 CSVHelper 操作 csv 文件, .net core, .net framework 读取写入 csv 文件
C# 实现字符串文本换行的方法,文本如何换行
C# 如何循环读取文件每一行文本内容
C# DateTime AddMonths 的错误用法导致跳过日期
C# 全角转换半角,半角转换为全角