3

I have two databound listboxes. The first only shows items that have been assigned to my product. The second listbox shows all available items. What I want to do is select all of the items in listbox 2 that list box one contains.

For example:
ListBox1-
Item 1
Item 3

ListBox2-
Item 1 (Selected)
Item 2
Item 3 (Selected)

Code I have:

List<string> myList = new List<string>();
            foreach(ListItem f in ListBoxSourceDetail.Items)
            {
                myList.Add(f.Value);
            }
            myList.ForEach(delegate(string n)
            {
                ListBoxSourceEdit.SelectedValue = n;
            });

1 Answer 1

5

I figured it out, I was over thinking it... Loop through each list item in the first box and then find each matching result in the second table to be selected.

foreach(ListItem i in ListBoxSourceDetail.Items)
        {
            ListBoxSourceEdit.Items.FindByText(i.ToString()).Selected = true;

        }
Sign up to request clarification or add additional context in comments.

2 Comments

You probably should be finding by value not text. Typically the value is unique where that isn't necessarily the case with the text.
@Adam Spicer, I agree, but in this case it is ok because the items come from a table that won't allow duplicate values.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.