33

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


在我的一个表单上(在 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;
4

6 回答 6

76

也许您也可以尝试此解决方案,只需为第二个组合框分配一个新的上下文:

                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";

谢谢

于 2012-04-20T01:22:46.697 回答
16

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

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

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

尽管如此,奇怪的行为......这只是一个错误,imo。

于 2011-01-05T14:53:46.123 回答
8

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

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());
于 2010-12-03T10:54:29.690 回答
2

是的,您是正确的,因为您绑定到同一个源,所以选择一个将应用于组合框的其余部分

为了克服这个问题,您需要在 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;
    }
于 2010-12-03T10:14:56.473 回答
2

Beth Massi 写了一篇文章解释了这个问题和正确的解决方案: https ://web.archive.org/web/20190114100843/https://blogs.msdn.microsoft.com/bethmassi/2007/09/19/binding-多个组合框到相同的数据源/

她还链接到了一系列关于数据绑定的其他视频。

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

在组合框上创建一个新的 BindingContext 似乎会破坏它。我建议按照 Beth 的解释:进行全新的 BindingSource 设置。

于 2018-09-21T14:28:46.830 回答
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();
于 2013-09-10T07:24:53.057 回答