.Net ComboBox Binding Issue

I have 2 comboboxes, each connected to the same DataTable as follows:

    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 the button to insert the record into the database, I use channelType.SelectedValue ... which returns the wrong value. I have a feeling that this is due to the use of the ComboBox class (which I set in the properties of the control in the design view). Has anyone encountered this problem?

This is programmed for a winforms application using C #

Edit:

For example, my Datatable stores values, for example:

channelType    channelTypeID
Web             2
Mailer          3
Catalog         4

This is sorted in a combobox, and when I select the first item (which will be “Catalog” when sorting), SelectedValue returns 2, when I select the second item, it returns 3 .... I would expect “Catalog” to return 4

+3
5

MSDN ComboBox.Sorted

,

Sorted ArgumentException. .

( )

ComboBox.sort DefaultView DataTable:

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

, , , selectedValue

+5

channelTypeCB.SelectedValue , newSKChanTypeCB.SelectedValue( , ).

+1

-: 2 BindingSource, DataSet, DataSource BindingSource.

+1

Sorted ComboBox, DataTable.

Sorted , ComboBox DisplayMember , DataTable. :

Data from a DataTable as a data source without sorting

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 by adding a database at the end of the query: "ORDER BY FieldName" http://technet.microsoft.com/en-us/library/ms188385.aspx

0
source

Source: https://habr.com/ru/post/1719144/


All Articles