WinForms List Box SelectedValue not updating?

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
                                           select new
                                           {
                                               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;
Debug.WriteLine(variantUID);
                        if (variantCount == 0)
                            {
                                this.lbVariants.Enabled = false;
                                this.lbVariants.BackColor = System.Drawing.SystemColors.ScrollBar;
                            }
                    }
                }
            }

Open in new window

tmreiterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
When you have any type of control that has a DataSource property, you usually need to flush it before your repopulate it with new data:

this.lbVariants.DataSource = null;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tmreiterAuthor Commented:
The code posted below works, and I've included your suggested line, although I think that the main problem was a now-fixed problem with my query.

In any event, thanks for the suggestion.

            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
                                           select new
                                           {
                                               baseUID = Variant.BaseUID,
                                               variantUID = Variant.VariantID,
                                               unitDesc = BaseUnitList.UnitDesc
                                           };
                        this.lbVariants.DataSource = null;
                        this.lbVariants.DataSource = queryVariant.ToList();
                        this.lbVariants.DisplayMember = "UnitDesc";
                        this.lbVariants.ValueMember = "VariantUID";
                        this.lbVariants.ClearSelected();

                        //CHECK TO SEE IF THERE ARE SUBVARIANTS; IF NOT, DISABLE LISTBOX
                        var findAlt = context.BaseUnitLists.Find(variantUID);
                        if (findAlt == null)
                            {
                                this.lbVariants.Enabled = false;
                                this.lbVariants.BackColor = System.Drawing.SystemColors.ScrollBar;
                            }
                    }
                }
            }

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.