views:

3363

answers:

6

I am having a hard time figuring out how to code a series of "if" statements that search through different dropdownlists for a specific value entered in a textbox. I was able to write code that finds a specific value in each dropdownlist; but, before this happens, I need to add an "if" statement saying, "if dropdownlist doesn't contain the specific value, go to next if statement, and so on". The following is an example of what I have so far:

if (dropdownlist1.SelectedValue == textbox1)
{
  dropdownlist1.SelectedIndex = dropdownlist1.items.indexof(dorpdownlist1.items.findbyvalue(textbox1.text) ...

if (dropdownlist2.SelectedValue == textbox1)
{
  dropdownlist2.SelectedIndex = dropdownlist2.items.indexof(dorpdownlist2.items.findbyvalue(textbox1.text) ...

etc...

What this does is reads or scans the first value or index in each dropdownlist, based off of my entry in textbox1. Unfortunately, it only identifies the first value or index. I need to figure out how to scan through the entire dropdownlist for all values per each "if" statement to find the matching textbox1 value. Does anyone have any suggestions?

Thank you,

DFM

A: 

You can use Linq to query your data instead of using if. :)

Marc Vitalis
+2  A: 
foreach (ListItem li in dropdownlist1.Items)
{
    if (li.Value == textBox1.text)
}

That is my suggestion for how to see if the value is in the dropdownlist.

JB King
Thanks! - This solution works perfectly
+1  A: 

I would make a list of Drop-down boxes and then use linq to select on it.

List<DropDownList> list = new List<DropDownList>();
list.Item.Add(dropdown1);
list.Item.Add(dropdown2); 
.... (etc)

var selected = from item in list.Cast<DropDownList>()
               where item.value == textBox1.text
               select item;
Eldila
Thanks for the response - I am not to confident/familiar with linq, at this point in my skills.
+4  A: 

The DropDownList inherits the Items collection from the ListControl. Since Items is an Array, you can use this syntax:

dropdownlist1.Items.Contains(textbox1.Text) as a boolean.

Drew McGhie
Thanks for the response - I thought "items.contains" only worked for ListBoxes. I'll look into this.
A: 

One line of code- split for readablilty.

this.DropDownList1.SelectedItem = this.DropDownList1.Items
     .SingleOrDefault(ddli => ddli.value == this.textbox1.value);
tom.dietrich
A: 

If you don't want to use LINQ:

        List<ComboBox> dropDowns = new List<ComboBox>();
        dropDowns.Add(comboBox1);
        dropDowns.Add(comboBox2);

        bool found = false;
        ComboBox foundInCombo = null;
        int foundIndex = -1;

        for (int i = 0; i < dropDowns.Count && found == false; i++)
        {
            for (int j = 0; j < dropDowns[i].Items.Count && found == false; j++)
            {
                if (item == textBox1.Text)
                {
                    found = true;
                    foundInCombo = dropDowns[i];
                    foundIndex = j;
                }
            }
        }
Martin Brown