Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

winforms - Multiple Combo Boxes With The Same Data Source (C#)


UPDATE: This is now resolved, see answer below.


On one of my forms (in a Windows Forms application) I have 3 Combo Boxes. These combo boxes need to display a list of prices (In text, with an integer back-end value).

All of these combo boxes are using the same data source (A List<> of type TSPrice, with ValueMember set to Price and DisplayMember set to Description).

My problem is this... Everytime I choose a price option from one of the dropdowns, they ALL change to the same value... Is this something to do with them all being bound to the same data source?

Here is how I am binding them:

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;

Edit: The issue was that they were all being bound to the same DataSource as confirmed by Saurabh. This is how I solved it.

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

probably you could also try this solution, just assign a new Context to the 2nd combo box:

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

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...