赋值 放在Form_Load中:
List<MData> merchantList = (List<MData>)oD;
comboBox1.DataSource = merchantList; //merchantList为List集合对象
comboBox1.DisplayMember = "merchantName";
comboBox1.ValueMember = "id";
取值:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string checkSId = comboBox2.SelectedValue + ""; //获取ValueMember 值
string index = comboBox2.SelectedIndex + ""; //获取拉下类表索引index
string ipStr = comboBox2.Text; //获取DisplayMember 值
}
MData类的声明:
class MData
{
private string merchantName;
private string id;
public string MerchantName
{
get { return merchantName; }
set { merchantName = value; }
}
public string Id
{
get { return id; }
set { id = value; }
}
public MData()
{
}
public MData(string merchantName,string id)
{
this.merchantName = merchantName;
this.id = id;
}
}
本文详细介绍了如何在C#中将ComboBox控件与List集合进行结合使用,包括在Form_Load事件中设置DataSource,以及如何通过SelectedIndex、SelectedValue和Text属性来获取用户的输入。同时,展示了MData类的定义,用于存储和展示如商户名称和ID等数据。
7789

被折叠的 条评论
为什么被折叠?



