Hello everyone out there! I'm trying to define the Valuemember and the Datasource of a Listbox Called "lb_Kostenstellen" (I need the ID of the Items for updating the Item in my Database) with the following statement:
lb_Kostenstellen.DataSource = db.Kostenstellen.ToList();
lb_Kostenstellen.ValueMember = "ID";
lb_Kostenstellen.DisplayMember = "Kostenstelle";
Now i created a button called "bt_Hinzufügen". When i click on the button, the Selected Item should move from my listbox called "lb_Kostenstellen" to "lb_Ausgewählt". (The item should be not visible in the first Listbox). I use this statement:
lb_Ausgewählt.Items.Add(lb_Kostenstellen.SelectedItem);
Until now, the statement works.
But now my Question: If i "switch" 3 Items from one Listbox to the Other,and then clicking on a button, I want to display 3 Messageboxes with the ID of every unique Item.
It doesn't work. I only get in the Messageboxes that:
MB : (NOTHING)
for (int i = 0; i < lb_Ausgewählt.Items.Count; i++) { MessageBox.Show(lb_Ausgewählt.ValueMember[i].ToString()); }
I hope you have a compatible solution for me. Thanks, Daniel
My DatabaseContext is "db" (For EntityFramework).
You are iterating through the string "ID" as lb_Chosen.ValueMember is a string with value "ID".
You should use something like the code below to get your IDs:
for (int i = 0; i < lb_Chosen.Items.Count; i++)
{
MessageBox.Show((lb_Chosen.Items[i] as YourAccountClass).ID.ToString());
}
Edit: translated names in code to english