user2849380
user2849380

Reputation: 49

C# select item of combobox by ID

I have this class

   class ComboboxValue
    {
        public int Id { get; private set; }
        public string Name { get; private set; }

        public ComboboxValue(int id, string name)
        {
            Id = id;
            Name = name;
        }

        public override string ToString()
        {
            return Name;
        }

    }

and I set my entries like this:

 var list = Funktionen.LoadCustomers();
        foreach (var item in list)
        {
            MyCombo.Properties.Items.Add(new ComboboxValue(item.ID, item.Name));
        }

In another function, I will set a item in my combobox by customerID. How can I do this? Btw. I´m using Devexpress.

Thank you.

Upvotes: 0

Views: 1927

Answers (4)

user14641417
user14641417

Reputation: 11

To select item in ComboboxValue class :

comboBox1.SelectedItem = comboBox1.Items.Cast<ComboboxValue>()
    .Where(i => i.Name == dataGridView1.CurrentRow.Cells[5].Value.ToString()).Single();

Upvotes: 1

bimfh
bimfh

Reputation: 48

Try MyCombo.SelectedItem = MyCombo.Items.SingleOrDefault(x => (x as ComboboxValue).Id == externalID)

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18290

To programmatically select a value for the combo, set the ComboBoxEdit.EditValue property. Here is some sample code:

ComboBoxEdit.EditValue = 2;  // select an item which ID = 2

Besides the Selected index you can use the SelectedItem property to select any item within the editor's item list. You need to assign an underlying data object to the SelectedItem property.

Alternatively, you can set its EditValue to '25' that is the ValueMember property value of the desirable item as shown in above example.

Reference these:
Select Item in ComboBoxEdit
how set combobox selected value

Upvotes: 1

onur
onur

Reputation: 374

var item = MyCombo.Properties.Items.FirstOrDefault(i => i.ID == yoursearchIDhere);

item will be combobox item which you want to get. If you look for it or not, let me know and explain clearly please LoadCustomers() should return List also.

Upvotes: 0

Related Questions