赋值 放在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;
}
}