12

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

itsmatt
  • 31,265
  • 10
  • 100
  • 164

9 Answers9

27
foreach (ListItem li in dropdownlist1.Items)
{
    if (li.Value == textBox1.text)
    {
       // The value of the option matches the TextBox. Process stuff here.
    }
}

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

JB King
  • 11,860
  • 4
  • 38
  • 49
  • Thnx a ton ! It saved me a lot of time – Kings Apr 27 '11 at 09:42
  • What if I wanted to put them in an array? – SearchForKnowledge Jan 26 '15 at 13:55
  • 1
    @SearchForKnowledge, put what into an array? Inside the "if" one could stuff the dropdown options into an array easily enough. – JB King Jan 26 '15 at 17:20
  • Each option from the dropdownlist. I actually ended up using List. Thanks for the reply. – SearchForKnowledge Jan 26 '15 at 18:15
  • I have two lists. I would like to add the selected item value in one list and selected item in another list, for each item in the dropdownlist. How can I do that? – Si8 Jan 06 '16 at 20:53
  • 1
    @SiKni8, you could likely pull the selected items from each list and add to the items in the dropdownlist. Otherwise looping through each list would be the brute force way. If neither of these work post the question as a new one to get better results rather than hijacking an answer I gave over 6 years ago please. – JB King Jan 06 '16 at 20:56
7

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
  • 1,086
  • 1
  • 12
  • 26
  • Thanks for the response - I thought "items.contains" only worked for ListBoxes. I'll look into this. –  May 15 '09 at 15:08
3

You can simply do like this.

if (ddl.Items.FindByValue("value") != null) {
   ddl.SelectedValue = "value";
};
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
Iyyappan
  • 171
  • 1
  • 5
2

The solutions presented work if you want to search for an exact value in a loaded combobox.

This solution, searches for partial values also. It uses a search button and the text portion of the dropdown box as the search criteria

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    ' query the dropdown object
    ''''''''''''
    ' part 9457 is a "contains" sample
    ' part 111 is a "startswith" sample

    Dim query As IEnumerable(Of [Object]) = _
    From item In cboParts.Items _
    Where (item.ToString().ToUpper().StartsWith(cboParts.Text.ToUpper())) _
    Select (item)

    ' show seached item as selected item
    cboParts.SelectedItem = query.FirstOrDefault()

    ' "startswith" fails, so look for "contains" 
    If String.IsNullOrEmpty(cboParts.SelectedItem) Then

        Dim query1 As IEnumerable(Of [Object]) = _
        From item In cboParts.Items _
        Where (item.ToString().ToUpper().Contains(cboParts.Text.ToUpper())) _
        Select (item)

        ' show seached item as selected item
        cboParts.SelectedItem = query1.FirstOrDefault()

        If String.IsNullOrEmpty(cboParts.SelectedItem) Then
            MsgBox("Part is not in dropdown list")
        End If

    End If
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Mike
  • 21
  • 1
2

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
  • 15,426
  • 23
  • 58
  • 62
  • Thanks for the response - I am not to confident/familiar with linq, at this point in my skills. –  May 15 '09 at 15:06
1

I was trying to find item by text in dropdownlist. I used the code below, it works: )

ListItem _lstitemTemp = new ListItem("Text To Find In Dropdownlist");
if(_DropDownList.Items.Contains(_lstitemTemp))
{
    dosomething();
}
ThrasHate
  • 11
  • 2
0
while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
{
    if (dropdownlist1.SelectedValue == textBox1.text)
    {
      // Add your logic here.
    }
}

//resetting to 0th index(optional)
dropdownlist1.SelectedIndex = 0;
Hirumina
  • 738
  • 1
  • 9
  • 23
  • 1
    Although this code might solve the problem, a good answer should explain **what** the code does and **how** it helps – BDL Jul 20 '20 at 12:37
0

One line of code- split for readablilty.

this.DropDownList1.SelectedItem = this.DropDownList1.Items
     .SingleOrDefault(ddli => ddli.value == this.textbox1.value);
tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
0

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
  • 24,692
  • 14
  • 77
  • 122