欢迎来到忆约科技! 手机版

C# 社区推荐

当前位置:首页 > 编程专栏 > C# > 正文

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

编辑时间:2020年08月06日 10:07 作者:E小编

新建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());
        }
    }

推荐采用方式一


二维码