更新:现在已解决,请参阅下面的答案。
在我的一个表单上(在 Windows 窗体应用程序中)我有 3 个组合框。这些组合框需要显示价格列表(在文本中,具有整数后端值)。
所有这些组合框都使用相同的数据源(TSPrice 类型的 List<>,ValueMember 设置为 Price,DisplayMember 设置为 Description)。
我的问题是……每次我从下拉列表中选择一个价格选项时,它们都会更改为相同的值……这与它们都绑定到同一个数据源有关吗?
这是我绑定它们的方式:
var priceList = new List<TSPrice>
{
new TSPrice(0, ""),
new TSPrice(0, "Half Day"),
new TSPrice(0, "Full Day"),
new TSPrice(0, "1 + Half"),
new TSPrice(0, "2 Days"),
new TSPrice(0, "Formal Quote Required")
};
objInsuredPrice.DataSource = priceList;
objTPPrice.DataSource = priceList;
objProvSum.DataSource = priceList;
objInsuredPrice.ValueMember = "Price";
objTPPrice.ValueMember = "Price";
objProvSum.ValueMember = "Price";
objInsuredPrice.DisplayMember = "Description";
objTPPrice.DisplayMember = "Description";
objProvSum.DisplayMember = "Description";
objInsuredPrice.SelectedIndex = 0;
objTPPrice.SelectedIndex = 0;
objProvSum.SelectedIndex = 0;
//objInsuredPrice.DataSource = objTPPrice.DataSource = objProvSum.DataSource = priceList;
//objInsuredPrice.ValueMember = objTPPrice.ValueMember = objProvSum.ValueMember = "Price";
//objInsuredPrice.DisplayMember = objTPPrice.DisplayMember = objProvSum.DisplayMember = "Description";
//objInsuredPrice.SelectedIndex = objTPPrice.SelectedIndex = objProvSum.SelectedIndex = 0;
编辑:问题是它们都被绑定到 Saurabh 确认的同一个数据源。这就是我解决它的方法。
var priceList = new List<TSPrice>
{
new TSPrice(0, ""),
new TSPrice(1, "Half Day"),
new TSPrice(2, "Full Day"),
new TSPrice(3, "1 + Half"),
new TSPrice(4, "2 Days"),
new TSPrice(5, "Formal Quote Required")
};
var insuredList = new TSPrice[5];
var TPList = new TSPrice[5];
var provList = new TSPrice[5];
priceList.CopyTo(insuredList);
priceList.CopyTo(TPList);
priceList.CopyTo(provList);
objInsuredPrice.DataSource = insuredList;
objTPPrice.DataSource = TPList;
objProvSum.DataSource = provList;
objInsuredPrice.ValueMember = objTPPrice.ValueMember = objProvSum.ValueMember = "Price";
objInsuredPrice.DisplayMember = objTPPrice.DisplayMember = objProvSum.DisplayMember = "Description";
objInsuredPrice.SelectedIndex = objTPPrice.SelectedIndex = objProvSum.SelectedIndex = 0;