Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Trying to get a ComboBox to set a value to the currently selected index
 

Trying to get a ComboBox to set a value to the currently selected index

Hi,
Using VS2008, targetting framework 3.5, programming in VB.net.

I have an iBindingList object, which I wish to use as the DataSource of a set of ComboBoxes.

I have a UserControl which contains 8 ComboBoxes, each of which uses the same DataSource; the iBindingList.

On this Control, when changing the selected value in any of the ComboBoxes, all of the ComboBoxes show the changed value, i.e. all of the ComboBoxes change at the same time, even though only one is being directly operated on.

I can't find anything in my code to show why this would be.

Further to my investigations, I have created a subclass of ComboBox, in which I set a breakpoint on a ValueChanged handler, which is fired 8 times on a change to one of the ComboBoxes, suggesting the issue is originating from within the ComboBox class.
Also, when placing two copies of my UserControl (16 ComboBoxes in total) onto a test form, both sets of 8 all change at the same time.

I vaguely have a recollection that I have had a similar problem to this in the past, but that was with DataGridViewComboBoxCells, and I got around it by overriding the GetValue and SetValue methods, which are not available for this flavour of ComboBox.

Obviously, what I am looking for is a method to make all of the ComboBoxes act independently of each other without simply copying the DataSource for every single ComboBox.

Any ideas?
KFrostILEM  Wednesday, September 09, 2009 2:44 PM
Also, I've just tried this using ten comboboxes on an otherwise empty form, and given them all the same datasource.
They still all change in unison!
How is this even a "feature"?

If I were to have a bunch of ComboBoxes, all with a dataSource of {"True", "False"}... They would still all change at the same time? Why? What for?

Must I really create a new copy of the dataSource separately for every single ComboBox? It's not a small list. This will use up large amounts of memory completely unneccessarily!
KFrostILEM  Wednesday, September 09, 2009 3:04 PM
So I'm no expert, but no one else has said anything so I will share my thoughts...
I am guessing that the binding list is actually the thing that is tracking what is selected... and that the combo boxes are just reading that...
Cause really, who would write code to keep all combo boxes with the same data source in sync...
So if I am right, yes copies would work, but what about creating a binding source object that ties to the bindinglist... the list can hold that data, and the binding source can hold the unique selection...
of course how to do this is a whole 'nother beast... but figured I would share that thought...

Randell
RandellP  Friday, September 11, 2009 8:41 PM

I have not used an IBindingList object for my ComboBoxes DataSource, but I have used DataTables. And yes, in order to use multiple ComboBoxes using the same DataTable for a DataSource, it is necessary to make a copy. So, what I wouldhave inmy ComboBox class issomething likea SetDataSource method; something like this:

public void SetDataSource(DataTable dt, string DisplayName, string ValueName)
{
	this.DisplayMember = DisplayName;
	this.ValueMember   = ValueName;
	this.DataSource = dt.Copy();
}

I would have given you an example with an IBindingList object, but since I haven't used that in this manner before, I don't know if it has a Copy command or how it would be used. From your post, it sounds like you know how to do that, so I'll leave that part for you to do.

But, I mainly wanted to point out that this isthe way ComboBoxes have always worked since the old .NET 1.0 days and now even into the current version of .NET.


~~Bonnie Berent [C# MVP]
BonnieB  Saturday, September 12, 2009 4:21 AM
Try this:

Fill each combobox datasource with the dataset seperatly.

combobox1.DataSource = ds.Tables["Customers"];
combobox1.DisplayMember = "CustomerName";
combobox1.ValueMember = "CustomerID";
Rohini Chavakula  Saturday, September 12, 2009 5:29 AM
That is exactly what the OP saidis NOT working and why I said that you have to use a copy. There isNO way around it:

combobox1.DataSource = ds.Tables["Customers"].Copy();
combobox1.DisplayMember = "CustomerName";
combobox1.ValueMember = "CustomerID";

~~Bonnie Berent [C# MVP]
BonnieB  Sunday, September 13, 2009 11:14 PM
Thanks for your replies.

I guess I'm going to have to look at hacking something together using the ComboBoxRenderer class, because the whole point of creating an iBindingList is that the contents are liable to change.
If I then had to manage each of the dataSource iBindingLists individually, then I will have lost all of the convenience of using an iBindingList over multiple ComboBoxes.
I still find this functionality mystifying but I guess I'll just have to stick it in the "it's not a bug, it's a feature" pile and work around it.
KFrostILEM  Monday, September 14, 2009 8:05 AM
Yes, you do need to create a new copy of datasource, but you don't have copy the data . Take a look at this code:

Dim dt As New DataTable()
dt.Columns.Add("Id")
dt.Columns.Add("Data")
dt.Rows.Add("1", "row 1")
dt.Rows.Add("2", "row 2")
dt.Rows.Add("3", "row 3")
dt.Rows.Add("4", "row 4")
ComboBox1.DisplayMember = "Data"
ComboBox1.ValueMember = "Id"
ComboBox1.DataSource = New BindingSource(dt, "")
ComboBox2.DisplayMember = "Data"
ComboBox2.ValueMember = "Id"
ComboBox2.DataSource = New BindingSource(dt, "")
Michal Burger  Monday, September 14, 2009 8:18 AM
Yeah.
I've discovered that the ComboBoxRenderer won't actually work at all if the user has the XP Windows Classic theme enabled, and I can't really specify to them that they have to use the horrible, ugly and dreadful "Tellytubby Windows" XP default themes, because aside from anything else, that would just be mean.

I don't mind Vista Aero, but I still refuse to use XP in anything other than Classic mode. You can't even configure the window colours, for cryin' out loud; it's just blue, grey or green, as far as I've ever been able to tell.


Anyway, yes, I guess I'm going to have to make alterations to the class of the datasource rather than getting to play with the comboboxrenderer. Shame.
KFrostILEM  Monday, September 14, 2009 12:00 PM

You can use google to search for other answers

Custom Search

More Threads

• datagridview datetimepicker without time format problem
• Cell Copy/Row Copy
• Bug DataGridView: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
• TimeSpan in Datagridview
• How to change size of the selected row in DataGrid ?
• Creating DataAdapter to an Access MDB - Object reference not set to an instance of an object
• How to refresh DataGridView ONLY when there's a new record
• Update database after every change in datagrid
• dsiplay an update table
• CellVaildating event escaped?