Reputation: 2261
I have a ListBox
control that I am trying to add values to and then retrieve the key/value from later on. When I add more than one item it overwrites my previously added value. Can someone shed a little light on this?
var holder = new Dictionary<int, string>();
holder.Add(Int32.Parse(cmb_AgencyList.SelectedValue.ToString()),
cmb_AgencyList.Text);
lb_AgenciesAdded.DataTextField = "Value";
lb_AgenciesAdded.DataValueField = "Key";
lb_AgenciesAdded.DataSource = holder;
lb_AgenciesAdded.DataBind();
Upvotes: 0
Views: 319
Reputation: 203819
Use the line:
lb_AgenciesAdded.AppendDataBoundItems = true;
To have data bound items appended to the control rather than to have it clear out the data before binding the new data.
Upvotes: 0
Reputation: 709
You are creating a new dictionary each time. Make the dictionary at the class level, outside of the method, and then just add to it. Or you can initialize the dictionary with the values in the listbox before adding to it.
Upvotes: 1