c# winform ComboBox控件 绑定数据,获取选中数据

本文介绍如何在WinForm中使用ComboBox控件绑定数据模型Region,并通过两种方式获取选中项的值和索引。示例展示了从初始化数据到响应选择事件的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先:新建winform窗体,并把ComboBox控件拖到窗体内

第一步:声明数据模型类-Region


    /// <summary>
    /// 地区
    /// </summary>
  public class Region
    {
        /// <summary>
        /// 地区ID
        /// </summary>
        public int id {get;set;}
        /// <summary>
        /// 地区名称
        /// </summary>
        public string name {get;set;}
    }

第二步:声明泛型集合 region

 List<Region> region=new List<Region>();

第三步:用初始化器,初始化数据,并赋值给泛型集合

 private void _Region()
        {  
            region.Add(new Region { id = 1, name = "郑州" });
            region.Add(new Region { id = 2, name = "北京" });
            region.Add(new Region { id = 3, name = "上海" });
            region.Add(new Region { id = 4, name = "深圳" });
        }

第四步:页面加载绑定数据到ComboBox

private void Form3_Load(object sender, EventArgs e)
        {
            _Region(); 
            this.label1.Text=region.Count.ToString(); 
            
            this.comboBox1.ValueMember = "id";//value隐藏值
            this.comboBox1.DisplayMember = "name";//Display显示
            this.comboBox1.DataSource=region;

            foreach (Region item in region)
            {
                Console.WriteLine(item.id);
                Console.WriteLine(item.name);
                Console.WriteLine("-------------");

            }
            Console.WriteLine();
        }

第五步:当选择数据后,响应此方法

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //第一种方法获取值
            string combobox1_value=this.comboBox1.Text;  
            string combobox1_index=this.comboBox1.SelectedValue.ToString();
            Console.WriteLine("combobox1_value==="+combobox1_value);
            Console.WriteLine("combobox1_index==="+combobox1_index);


            Console.WriteLine("===============分割线==================");

            //第二种方法获取值
           var Vcombobox1_value=this.comboBox1.SelectedItem as Region; 
            Console.WriteLine("name==="+Vcombobox1_value.name);
            Console.WriteLine("id==="+Vcombobox1_value.id);

        }

隆重感谢网友:“浅笑@微笑”,“亡五-男-苏州” ,“.嘀嗒.嘀嗒”

 

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForms1.ceshi
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
         List<Region> region=new List<Region>();
        private void Form3_Load(object sender, EventArgs e)
        {
            _Region(); 
            this.label1.Text=region.Count.ToString(); 
            
            this.comboBox1.ValueMember = "id";//value隐藏值
            this.comboBox1.DisplayMember = "name";//Display显示
            this.comboBox1.DataSource=region;

            foreach (Region item in region)
            {
                Console.WriteLine(item.id);
                Console.WriteLine(item.name);
                Console.WriteLine("-------------");

            }
            Console.WriteLine();
        }
        private void _Region()
        {  
            region.Add(new Region { id = 1, name = "郑州" });
            region.Add(new Region { id = 2, name = "北京" });
            region.Add(new Region { id = 3, name = "上海" });
            region.Add(new Region { id = 4, name = "深圳" });
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //第一种方法获取值
            string combobox1_value=this.comboBox1.Text;  
            string combobox1_index=this.comboBox1.SelectedValue.ToString();
            Console.WriteLine("combobox1_value==="+combobox1_value);
            Console.WriteLine("combobox1_index==="+combobox1_index);


            Console.WriteLine("===============分割线==================");

            //第二种方法获取值
           var Vcombobox1_value=this.comboBox1.SelectedItem as Region; 
            Console.WriteLine("name==="+Vcombobox1_value.name);
            Console.WriteLine("id==="+Vcombobox1_value.id);

        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-极纪元JJYCheng

客官,1分钱也是爱,给个赏钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值