具有相同数据源的多个combobox(C#)


更新:现在已解决,请参阅下面的答案。


在我的一个表单(在Windows窗体应用程序中)我有3个combobox。 这些combobox需要显示价格列表(在文本中,带有整数后端值)。

所有这些combobox都使用相同的数据源(TSPrice类型的A List ,ValueMember设置为Price,DisplayMember设置为Description)。

我的问题是这样的…每次我从其中一个下拉菜单中选择一个价格选项时,它们都会变为相同的值…这是否与它们绑定到同一个数据源有关?

这是我如何约束他们:

var priceList = new List { 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确认绑定到相同的DataSource。 这就是我解决它的方式。

 var priceList = new List { 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; 

我知道你没有要求它,但我建议你稍微重构一下你的最终代码:-)

 private List GetPriceList() { return new List { 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") }; } private void BindPriceList(ComboBox comboBox, List priceList) { comboBox.DataSource = priceList(); comboBox.ValueMember = "Price"; comboBox.DisplayMember = "Description"; comboBox.SelectedIndex = 0; } BindPriceList(objInsuredPrice, GetPriceList()); BindPriceList(objTPPrice, GetPriceList()); BindPriceList(objProvSum, GetPriceList()); 

可能你也可以尝试这个解决方案,只需为第二个combobox分配一个新的Context:

  combobox1.DataSource = results; combobox1.DisplayMember = "DisplayValue"; combobox1.ValueMember = "Value"; combobox2.BindingContext = new BindingContext(); //create a new context combobox2.DataSource = results; combobox2.DisplayMember = "DisplayValue"; combobox2.ValueMember = "Value"; 

谢谢

我不明白为什么这应该这么难……你可以将它们链接到相同数据源的克隆……这样就解决了问题。 你需要做的就是

 objInsuredPrice.DataSource = new List(priceList); objTPPrice.DataSource = new List(priceList); objProvSum.DataSource = new List(priceList); 

顺便说一下,这正是VVS的代码所做的。

仍然,奇怪的行为……只是必须是一个bug,imo。

是的,绝对是正确的,因为您绑定到相同的源,因此选择一个将应用于其余的combox

为了克服这个问题,你需要在slectedindex更改事件中手动删除其他combobox处理程序然后设置所选索引然后再添加处理程序以放入代码,只需看下面

  ComboBox c1 = new ComboBox(); ComboBox c2 = new ComboBox(); c1.SelectedIndexChanged += new EventHandler(c1_SelectedIndexChanged); c2.SelectedIndexChanged += new EventHandler(c2_SelectedIndexChanged); void c2_SelectedIndexChanged(object sender, EventArgs e) { c1.SelectedIndexChanged -= c1_SelectedIndexChanged; c2.SelectedIndex = 2; c1.SelectedIndexChanged += c1_SelectedIndexChanged; } void c1_SelectedIndexChanged(object sender, EventArgs e) { c2.SelectedIndexChanged -= c2_SelectedIndexChanged; c1.SelectedIndex = 2; c2.SelectedIndexChanged += c2_SelectedIndexChanged; } 

Beth Massi写了一篇文章解释这个问题和正确的解决方案: https : //blogs.msdn.microsoft.com/bethmassi/2007/09/19/binding-multiple-combo-boxes-to-the-same-data-资源/

她还有一系列关于数据绑定的video,她也链接到了。

我已经阅读了之前的答案并且可以确认,遗憾的是,当我尝试它们时,它们都没有用。

在combobox上创建一个新的BindingContext似乎打破了它。 我建议按照Beth的说法做:做一个全新的BindingSource设置。

这适用于我,我不需要复制源。

 List days = GetDays(); List months = GetMonths(); List years = GetYears(); Son1DDLDay.DataSource = days; Son1DDLDay.DataBind(); Son1DDLMonth.DataSource = months; Son1DDLMonth.DataBind(); Son1DDLYear.DataSource = years; Son1DDLYear.DataBind(); Son2DDLDay.DataSource = days; Son2DDLDay.DataBind(); Son2DDLMonth.DataSource = months; Son2DDLMonth.DataBind(); Son2DDLYear.DataSource = years; Son2DDLYear.DataBind();