Get ID of item selected in ComboBox

c# entity-framework linq winforms

Question

I possessComboBox It has entries from my database in it. I'm attempting to getID of the object that has been chosen in theComboBox nonetheless, nothing I have done seems to be working.

int id = cbbilar.SelectedIndex + 1;

This is how I now have it; however, it is very inefficient and becomes unusable if the database's initial contents are deleted.

var cars = (from z in db.Bilar 
            select new { Value = z.Id, Names = z.Marke.Namn + " "  + z.Namn }).ToList();
cbbilar.DataSource = cars;
cbbilar.DisplayMember = "Names";
cbbilar.ValueMember = "Value";
cbbilar.SelectedIndex = 0;

This is my security code.Combobox . How do I get it to get theID of theSelectedItem ?

1
3
10/28/2016 12:17:41 PM

Accepted Answer

ZZZ_tmp
2
10/28/2016 11:53:46 AM

Popular Answer

Here is what I accomplished:

comboBoxPickupLoc.DataSource = pickupLocationRepo.GetPickupLocations();
comboBoxPickupLoc.DisplayMember = "LocationName";
comboBoxPickupLoc.ValueMember = "Id";
comboBoxPickupLoc.SelectedIndex = -1;

then, as seen below, you may get the Id value.

  object value = comboBoxPickupLoc.SelectedValue;
  MessageBox.Show(value.ToString());

Thanks.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow