valuemember in combobox

lional

Well-known member
Joined
Nov 29, 2018
Messages
60
Programming Experience
Beginner
Morning all
In my student administration app I had some comboboxes where the display member and the value member differed so I used the following code which seems to work fine
C#:
// ### populate combobox for Remembering Rating Code ###
            // Create a List to store our KeyValuePairs
            string QueryRememberingRatingCode = @"select HFRatingId, HFRatingDescription from HealthFunctioning";
            SqlCommand cmdRememberingRatingCode = new SqlCommand(QueryRememberingRatingCode, conn);
            List<KeyValuePair<string, string>> RememberingRatingData = new List<KeyValuePair<string, string>>();
            SqlDataReader rdrRememberingRatingCode = cmdRememberingRatingCode.ExecuteReader();

            //// Loop through the result set
            while (rdrRememberingRatingCode.Read())
            {
                RememberingRatingData.Add(new KeyValuePair<string, string>(rdrRememberingRatingCode["HFRatingId"].ToString(), rdrRememberingRatingCode["HFRatingDescription"].ToString()));
            }
            // Clear the comboBox

            cboRememberingRatingCode.DataSource = null;
            cboRememberingRatingCode.Items.Clear();

            // Bind the combobox
            cboRememberingRatingCode.DataSource = new BindingSource(RememberingRatingData, null);
            cboRememberingRatingCode.DisplayMember = "value";
            cboRememberingRatingCode.ValueMember = "Key";

            // Close home Remembering rating Code reader
            rdrRememberingRatingCode.Close();
What I am struggling with is the following
When the student profile gets edited I need the combobox item that was written to the database to be automatically selected.
I tried using selected value and it doesn't give an error but doesn't give the expected results. I tried using valuemember but I get a "Cannot bind to the new value member.
Parameter name: value" error

Enjoy your day everyone
 
The ValueMember is the name of a property that all items in the data source have. The SelectedValue is the value of that named property in the selected item. If you want to select a specific item then setting the ValueMember can't possibly help you. You need to set the SelectedValue. You have set the ValueMember to "Key" so you need to assign the Key property value of the item you want selected to the SelectedValue. If the expected item wasn't selected then you didn't do that.
 
In addition to what jmcilhinney said, I believe the DisplayMember and ValueMember properties have to be set before the DataSource property.

-Paul
 
In addition to what jmcilhinney said, I believe the DisplayMember and ValueMember properties have to be set before the DataSource property.

-Paul

They don't have to be but they should be. The reason for that is that setting the DataSource will cause the items to be displayed in the control. If you set the DataSource first then the items will be displayed without a DisplayMember and then will have to be redisplayed when you set the DisplayMember. By setting the DisplayMember first, the items are displayed only once and correctly that one time.

As an exception to this, some time ago I found that the CheckedListBox behaved oddly if you set the DisplayMember first. That may have changed since then but it is something to consider.
 
Thank you everyone for their replies, I made the necessary corrections and all is good now

Really appreciate all the help
 
Back
Top Bottom