I've working on a C# Winforms listbox which populates normally as per the code below, but only once. The next time I double-click on the listbox, I get the original valuemember, not the value member of the newly-selected item. I'm using Enity Framework and Visual Studio Express 2013.
This problem arises in two contexts:
1) The items in the listbox represent variants of an item selected in a tree view; as mentioned, this part of the process works fine as per some code which is not shown. In the code shown below, a user can find a variant's subvariants by double-clicking the item in the list box. This also works correctly. But if I double-click on the listbox again to drill down to the subvariant's variants, the listbox continues to only show the subvariants. Here is how the process should work:
A) I select an item in a treeview ("Item X"), and the listbox is populated with the selected item's variants ("Item X1, Item X2, Item X3"). This works correctly.
B) If I double-click a variant in the list box ("Item X1"), the listbox repopulates to show Item X1's subvariants ("Item X1a, Item X1b"). This also works correctly.
C) If I double click on a subvariant in the listbox ("Item X1a"), the list box should either repopulate with the subvariant's variants, or if none exist, become disabled. This is the part that doesn't work (see code beginning with "//CHECK TO SEE...")--the listbox does not change, and the listbox's SelectedValue does not change from when Item X1 was selected, even though Item X1a is now selected in the listbox.
2) The problem also occurs is I clear and repopulate the treeview and select a new item in it--the original item's variants are shown in the listbox, not the new item's.
I've tried to fix this using Refresh(); Update(); DataSource = "", but all of them either had no effect or produced errors. I've also tried using the listbox's SelectedValueChanged Event, but get an error whenever I try; the error is something to the effect of "nothing of this type found" (sorry, error in Russian, not sure of exact translation).
I'm a hobbyist programmer and am new to both Entity Framework and C# (moving from Access/VBA), so hopefully I'm not missing anything obvious.
Here is my code:
private void lbVariants_DoubleClick(object sender, EventArgs e)
using (CMEntities context = new CMEntities())
if (this.lbVariants.SelectedIndex != -1)
string variantUID = this.lbVariants.SelectedValue.ToString();
var queryVariant = from Variant in context.Variants
join BaseUnitList in context.BaseUnitLists on Variant.VariantUID equals BaseUnitList.UID
where Variant.BaseUID == variantUID
baseUID = Variant.BaseUID,
unitDesc = BaseUnitList.UnitDesc,
//uid = BaseUnitList.UID
this.lbVariants.DisplayMember = "UnitDesc";
this.lbVariants.ValueMember = "BaseUID";
this.lbVariants.DataSource = queryVariant.ToList();
//CHECK TO SEE IF THERE ARE SUB-SUB-VARIANTS; IF NOT, DISABLE LISTBOX
var findAlt = context.BaseUnitLists.Find(variantUID);
int variantCount = findAlt.VariantCount;
if (variantCount == 0)
this.lbVariants.Enabled = false;
this.lbVariants.BackColor = System.Drawing.SystemColors.ScrollBar;