How to set selected value from Combobox?

265,206

Solution 1

Try this one.

cmbEmployeeStatus.SelectedIndex = cmbEmployeeStatus.FindString(employee.employmentstatus);

Hope that helps. :)

Solution 2

In order to do the database style ComboBoxes manually trying to setup a relationship between a number (internal) and some text (visible), I've found you have to:

  1. Store you items in a list (You are close - except the string,string - need int,string)
  2. DataSource property to the list (You are good)
  3. DataMember,DataValue (You are good)
  4. Load default value (You are good)
  5. Put in value from database (Your question)
  6. Get value to put back in database (Your next question)

First things first. Change your KeyValuePair to so it looks like:

(0,"Select") (1,"Option 1")

Now, when you run your sql "Select empstatus from employees where blah" and get back an integer, you need to set the combobox without wasting a bunch of time.

Simply: *** SelectedVALUE - not Item ****

cmbEmployeeStatus.SelectedValue = 3;   //or

cmbEmployeeStatus.SelectedValue = intResultFromQuery;   

This will work whether you have manually loaded the combobox with code values, as you did, or if you load the comboBox from a query.

If your foreign keys are integers, (which for what I do, they all are), life is easy. After the user makes the change to the comboBox, the value you will store in the database is SelectedValue. (Cast to int as needed.)

Here is my code to set the ComboBox to the value from the database:

if (t is DBInt) //Typical for ComboBox stuff
    {
    cb.SelectedValue = ((DBInt)t).value;
    }

And to retrieve:

((DBInt)t).value = (int) cb.SelectedValue;

DBInt is a wrapper for an Integer, but this is part of my ORM that gives me manual control over databinding, and reduces code errors.

Why did I answer this so late? I was struggling with this also, as there seems to be no good info on the web about how to do this. I figured it out, and thought I'd be nice and post it for someone else to see.

Solution 3

In windows Appliation we use like this

 DDLChangeImpact.SelectedIndex = DDLChangeImpact.FindStringExact(ds.Tables[0].Rows[0]["tmchgimp"].ToString());
 DDLRequestType.SelectedIndex = DDLRequestType.FindStringExact(ds.Tables[0].Rows[0]["rmtype"].ToString());

Solution 4

Below will work in your case.

cmbEmployeeStatus.SelectedItem =employee.employmentstatus;

When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.

EDIT

I think setting the Selected Item as below is incorrect in your case.

cmbEmployeeStatus.SelectedItem =**employee.employmentstatus**;

Like below

var toBeSet = new KeyValuePair<string, string>("1", "Contract");
cmbEmployeeStatus.SelectedItem = toBeSet;

You should assign the correct name value pair.

Solution 5

Use the SelectedIndex property for the respective employee status in the combo box.

Share:
265,206

Related videos on Youtube

thinzar
Author by

thinzar

Updated on November 17, 2020

Comments

  • thinzar
    thinzar over 3 years

    I use combobox in c# windows form. I bound the item list as below:

    var employmentStatus = new BindingList<KeyValuePair<string, string>>();
    
    employmentStatus.Add(new KeyValuePair<string, string>("0", "[Select Status]"));
    employmentStatus.Add(new KeyValuePair<string, string>("1", "Contract"));
    employmentStatus.Add(new KeyValuePair<string, string>("2", "Part Time"));
    employmentStatus.Add(new KeyValuePair<string, string>("3", "Permanent"));
    employmentStatus.Add(new KeyValuePair<string, string>("4", "Probation"));
    
    employmentStatus.Add(new KeyValuePair<string, string>("5", "Other"));
    cmbEmployeeStatus.DataSource = employmentStatus;
    cmbEmployeeStatus.ValueMember = "Key";
    cmbEmployeeStatus.DisplayMember = "Value";
    cmbEmployeeStatus.SelectedIndex = 0;
    

    I save the selected value in database eg.1 or 2. Now I want to set selected value from database item like:

    cmbEmployeeStatus.SelectedValue =employee.employmentstatus;     
    

    But combobox not selected with value. How can I do that?

    • thinzar
      thinzar almost 13 years
      I use all of your answer but combox does not appear with text.I bind the combobox in form_Load . I delete the binding the text appear in combobox . Why ?
    • CharithJ
      CharithJ almost 13 years
      I think the value you assign as the selectedItem is not correct. I've updated my answer below.
  • simi
    simi over 10 years
    You must try to cast this combobox Because C# is statically-typed at compile time, after a variable is declared, it cannot be declared again or used to store values of another type unless that type is convertible to the variable's type.
  • til_b
    til_b over 9 years
    Please also keep in mind there is FindString, which returns the first element starting with the given string, and FindStringExact which performs an exact string match.
  • Broots Waymb
    Broots Waymb over 6 years
    I'd lean more toward FindStringExact just to be on the safe side. FindString works in this case, but could break in cases where the list has similar items.