Chris Klepeis
Chris Klepeis

Reputation: 9983

.Net ComboBox Binding Issue

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

Upvotes: 3

Views: 1440

Answers (5)

pfigueroa
pfigueroa

Reputation: 541

The problem is the Sorted property of ComboBox because your data come from a DataTable.

When the Sorted property is used, ComboBox organizes the DisplayMember only and ignores another data as it can not modify the DataTable directly. Example:

Data from DataTable as DataSource without Sorted

channelType   channelTypeID  ComboBoxDisplayMember    ComboboxValueMember
Web            2              Web                       2
Mailer         3              Mailer                    3
Catalog        4              Catalog                   4

now with Sorted Property

channelType   channelTypeID  ComboBoxDisplayMemberSorted   ComboboxValueMember
Web            2              Catalog                       2
Mailer         3              Mailer                        3
Catalog        4              Web                           4

This problem can be solved in the database adding at end of query: "ORDER BY FieldName" http://technet.microsoft.com/en-us/library/ms188385.aspx

Upvotes: 0

NineBerry
NineBerry

Reputation: 28499

This is a known issue in .net

Please upvote this to have it fixed in .net 5: http://connect.microsoft.com/VisualStudio/feedback/details/542353/combobox-selectedvalue-returns-incorrect-data-when-sorted-is-true

Upvotes: 0

Chris Klepeis
Chris Klepeis

Reputation: 9983

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

Upvotes: 5

Mitch Wheat
Mitch Wheat

Reputation: 300549

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.

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75296

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).

Upvotes: 1

Related Questions