Search code examples
c#comboboxselectedvalue

C3 comboBox different display and value members but need to access both


I have a dataset that has multiple columns which include a Text value to display and a numeric value that I need to use for filtering an another combobox.

            MyComboBox.DisplayMember = "Reason";
            MyComboBox.ValueMember = "ReasonID";
            MyComboBox.DataSource = MyDataTable;

The issue I have is that part of the code I need the ID however for another part of the code I need the text. I can get the ID back but I'm not sure how to access the text when the value changes. I've tried the following

            String test1 = MyComboBox.SelectedValue.ToString();
            String test2 = MyComboBox.SelectedText.ToString();

Test1 is the ID as I expected. However test2 is "" and I can't see any properties that give the display value instead of the selected value.


Solution

  • Use ComboBox.Text Property

    string value = MyComboBox.Text;
    

    Text property contains value of DisplayMember of selected item in your case.

    About ComboBox.SelectedText from MSDN

    Gets or sets the text that is selected in the editable portion of a ComboBox.

    So this is not a text of selected item