首先:新建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);
}
隆重感谢网友:“浅笑@微笑”,“亡五-男-苏州” ,“.嘀嗒.嘀嗒”
c# winform ComboBox控件 绑定数据,获取选中数据_初始化
完整代码:
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);
}
}
}
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有