简体   繁体   中英

select combobox item by value C#

I have a combobox, bound to Datatable and have the following properties:

cboCars.DisplayMember = "carLiscen";
cboCars.ValueMember = "carNo";

How can I select the DisplayMember when I know the ValueMember ?

If you have a ValueMember set you can select using SelectedValue

cboCars.DisplayMember = "carLiscen";
cboCars.ValueMember = "carNo";

cboCars.SelectedValue = "valuemember value";

You can use cboCars.SelectedValue = "123"; property for this. Here's a code snippet which will show it in action.

    public void Test()
    {
        ArrayList info = new ArrayList();
        info.Add(new CarInfo { CarLiscen = 123456, CarNo = 123});
        info.Add(new CarInfo { CarLiscen = 234567, CarNo = 234 });
        cboCars.DataSource = info;

        cboCars.DisplayMember = "CarLiscen";
        cboCars.ValueMember = "CarNo";

        cboCars.SelectedValueChanged +=
            delegate(object sender, EventArgs e)
            {
                if (cboCars.SelectedIndex != -1)
                {
                    this.Text = cboCars.SelectedValue.ToString();
                }
            };

        cboCars.SelectedValue = 234;
    }

And if you wonder what is the definition of CarInfo . Here's its code (which is fairly simple):

public class CarInfo
{
    public int CarLiscen { get; set; }
    public int CarNo { get; set; }
}

Hope this helps.

You can search for the correct item and set it to that, very simple:

cbTEST.SelectedIndex = cbTEST.FindStringExact("your search string here");

or select an item based on an ListViewItem:

cbTEST.SelectedIndex = cbTEST.FindStringExact(lvTEST.SelectedItems[0].SubItems[0].Text);

thats it. very simple!

Hi Guys the best way if searching for a text or value is

int Selected;    
int count = ComboBox1.Items.Count;
    for (int i = 0; (i<= (count - 1)); i++) 
     {        
         ComboBox1.SelectedIndex = i;
        if ((string)(ComboBox1.SelectedValue) == "SearchValue") 
        {
            Selected = i;
        }

    }

    ComboBox1.SelectedIndex = Selected;

The problem is that the combobox expect the exact type. So if you for example use a datagridview and you see the value (int) in the field, you pass it to the SelectedValue property of the combo. But in fact you are not passing an integer, you are passing an object. That is what usually is going wrong. It my a while to sort this out, but finally I have found how to do it... How to solve this... easy:

For example, you have an ID (integer in the database), then you need to do something like this:

This is the one that I did and it does not work: cmbFilter.SelectedValue = dgvListOfFilters.Rows[intRowSelected].Cells[3].Value;

You should do this to make it work: int intCategory = Convert.ToInt32(dgvListOfFilters.Rows[intRowSelected].Cells[3].Value); cmbFilter.SelectedValue = intCategory;

Hope this will be helpfull for many people...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM