views:

225

answers:

3

I have 2 comboboxes, each are bound to the the same DataTable like so:

    channelTypeCB.DataSource = SynergyData.ds.Tables["ChannelTypes"];
    channelTypeCB.DisplayMember = "channelType";
    channelTypeCB.ValueMember = "channelTypeID";
    channelTypeCB.BindingContext = new BindingContext();

    newSKChanTypeCB.DataSource = SynergyData.ds.Tables["ChannelTypes"];
    newSKChanTypeCB.DisplayMember = "channelType";
    newSKChanTypeCB.ValueMember = "channelTypeID";
    newSKChanTypeCB.BindingContext = new BindingContext();

When I click a button to insert a record into the database, I use channelType.SelectedValue... which is returning the incorrect value. I have a feeling it has something to do with using the ComboBox sort (which I set in the properties of the control in the design view). Has anyone ran into this problem?

This is programmed for a winforms application using C#

Edit:

For example, my Datatable stores values like:

channelType    channelTypeID
Web             2
Mailer          3
Catalog         4

This is sorted in the combobox, and when I select the first item (which would be "Catalog" when sorted) the SelectedValue returns 2, when I select the second item it returns 3.... I would have expected "Catalog" to return 4

+1  A: 

You may be referring to channelTypeCB.SelectedValue in your code, when you need to be referring to newSKChanTypeCB.SelectedValue (this is a total guess based purely on your control names).

MusiGenesis
definately referencing the correct one (channelTypeCB.SelectedValue)... its almost as if the DisplayMember is getting sorted but the ValueMember isnt
Chris Klepeis
Try commenting out the ".BindingContext = New BindingContext();" lines. Not sure what that's doing, exactly, but it might be removing the normal BindingContext (the one that works) with an empty one that doesn't work.
MusiGenesis
IN fact thats exactly what its doing... I would have thought it only sorts on the DisplayMember? ... the ValueMember is sorted seperately?
Chris Klepeis
I have to create a new BindingContext so that when I change the selectedindex on one combobox, the other combobox doesnt display the same thing (I saw it done that way in a post by someone at MS)
Chris Klepeis
@Chris: there's no way a ComboBox should ever sort the DisplayMembers separately from the ValueMembers. Only Excel does things like that.
MusiGenesis
@Chris: I think a better way to keep the two ComboBoxes from always displaying the same thing is to bind the second ComboBox to a different DataTable that you Clone() from the first DataTable.
MusiGenesis
+1  A: 

I would do this differently: I would create 2 separate BindingSource's, each based on your DataSet, and then bind each controls DataSource to the respective BindingSource just created.

Mitch Wheat
Just updated my code to use this, didnt fix the selectedIndex problem, but thanks for the advice.
Chris Klepeis
selectedValue* I mean
Chris Klepeis
+2  A: 

MSDN ComboBox.Sorted

Probably related to this

Attempting to set the Sorted property on a data-bound control raises an ArgumentException. You must sort the data using the underlying data model.

(Wasn't getting any errors though)

So instead of using the ComboBox.sort, I'm sorting the DefaultView of the DataTable:

SynergyData.ds.Tables["ChannelTypes"].DefaultView.Sort = "channelType";

Not convinced this is the the best way to go about it, but it works, and now selectedValue returns the correct thing

Chris Klepeis
Nice Find. That seems a reasonable way to go...
Mitch Wheat