更新:现已解决,请参阅下面的答案。
在我的一个表单上(在Windows窗体应用程序中)我有3个组合框。这些组合框需要显示价格列表(在文本中,带有整数后端值)。
所有这些组合框都使用相同的数据源(A List<>类型为TSPrice,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所确认的绑定到同一个DataSource。这就是我解决它的方法。
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;
答案 0 :(得分:64)
可能你也可以尝试这个解决方案,只需为第二个组合框分配一个新的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";
谢谢
答案 1 :(得分:13)
我不明白为什么这应该这么难......你只需将它们链接到同一数据源的克隆......就可以解决问题。您需要做的就是
objInsuredPrice.DataSource = new List<TSPrice>(priceList);
objTPPrice.DataSource = new List<TSPrice>(priceList);
objProvSum.DataSource = new List<TSPrice>(priceList);
顺便说一句,这正是VVS的代码所做的。
仍然,奇怪的行为......只是 是一个错误,imo。
答案 2 :(得分:6)
我知道你没有要求它,但我建议你稍微重构一下你的最终代码: - )
private List<TSPrice> GetPriceList()
{
return 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")
};
}
private void BindPriceList(ComboBox comboBox, List<TSPrice> priceList)
{
comboBox.DataSource = priceList();
comboBox.ValueMember = "Price";
comboBox.DisplayMember = "Description";
comboBox.SelectedIndex = 0;
}
BindPriceList(objInsuredPrice, GetPriceList());
BindPriceList(objTPPrice, GetPriceList());
BindPriceList(objProvSum, GetPriceList());
答案 3 :(得分:2)
是的,绝对你是正确的,因为你绑定到同一个源,所以选择一个将应用于其余的combox
为克服这个问题,你需要在slectedindex更改事件中手动删除其他组合框处理程序,然后设置所选索引,然后再添加处理程序以放入代码,如下所示
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;
}
答案 4 :(得分:1)
Beth Massi写了一篇文章,解释了此问题和正确的解决方案: https://blogs.msdn.microsoft.com/bethmassi/2007/09/19/binding-multiple-combo-boxes-to-the-same-data-source/
她还链接了一系列有关数据绑定的其他视频。
我已经阅读了之前的答案,可以确认的是,当我尝试使用它们时,没有一个起作用。
在组合框上创建新的BindingContext似乎破坏了它。我建议按照Beth的说明进行操作:进行全新的BindingSource设置。
答案 5 :(得分:0)
这对我有用,我不需要复制来源。
List<string> days = GetDays();
List<string> months = GetMonths();
List<string> 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();