4

I have a ComboBox that gets filled with items from my database. I'm trying to get the ID of the item that is selected in the ComboBox, but nothing I've tried seems to be working.

int id = cbbilar.SelectedIndex + 1;

This is how I have it right now, it's very inefficient and stops working if the first items in the database are removed

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 the code for my Combobox. How do I make it fetch the ID of the SelectedItem?

0

5 Answers 5

3

You need to use SelectedValue and int.TryParse method. Like this:

int id;
bool result = int.TryParse(cbbilar.SelectedValue.ToString(), out id);
6
  • Can you not just cast SelectedValue directly to int as it passed in as int?
    – Ian of Oz
    Oct 28, 2016 at 11:42
  • @IanofOz The SelectedValue is of type object. Oct 28, 2016 at 11:43
  • This seems to have fixed it! Thanks a lot! How come I had to write it this way instead of just simply int id = int.Parse(cbbilar.SelectedValue.ToString()); ?
    –  Matt J
    Oct 28, 2016 at 11:51
  • @MattJ Check here to know more about int.TryParse msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx I also included the link to MSDN in my answer. Oct 28, 2016 at 11:53
  • @S.Akbari True, but given the SelectedValue object holds an int, you should be able to cast it back to an int. I just double checked - create a new object, assign it an int value and you can cast it to an int. Obviously relies on knowing you pass an int in initially.Matt, you choice if you want to try that
    – Ian of Oz
    Oct 28, 2016 at 11:55
1

Here is what I got done:

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

and then you can get the Id value as shown below

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

Thanks.

0

When ever I am grabbing the SelectedIndex value, I am doing so to place the id into another DataBase for various reasons. I have used the TryParse to do this task but the line below is about as simple as it gets by pulling it as a string rather than parcing to an int.

string id = combobox.SelectedValue.ToString();
0

Get ID of item selected in WPF ComboBox

Country & State are your model classes.

code =>

Country country_selected = (Country)DDCountry.SelectedValue;  
State state_selected = (State)DDState.SelectedValue;  
int country_id = country_selected.country_id;
int state_id = state_selected.state_id;      
0

I know this is an old question, but here is another way to take information form the db to the comboBox.

   private void userControl_Load(object sender, EventArgs e)
   {
      scon.Open(); //--DB Connection
      string query = "SELECT * FROM AssetTypes"; //Query

      SqlDataReader row;
      SqlCommand scmd = new SqlCommand(query,scon);
      row = scmd.ExecuteReader();

      if (row.HasRows)
      {
         while (row.Read())
         {
            AssetTypeComboBox.Items.Add(row["AssetTypesName"].ToString());
         }
      }
      scon.Close();
   }

to get the ID:

int itemID = AssetTypeComboBox.SelectedIndex + 1; //returns the ID number in the DB. provided the ;list was not sorted.

worked like a charm for me, maybe it can help others in future if they are struggling and do not understand much.

3
  • This doesn't answer the question. They know how to populate a combobox. The question is how to get an object's ID value from it. You don't even mention that, let alone show how it's done. Apr 28, 2022 at 7:28
  • @GertArnold , Hi , my apologies you are actually correct. i will edit it and amend it to return the actual ID as in the DB . Apr 28, 2022 at 11:35
  • SelectedIndex is not an object's ID value. This answer doesn't hold water. Apr 28, 2022 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.