DFM
DFM

Reputation:

How to Search Through a C# DropDownList Programmatically

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

Upvotes: 12

Views: 70888

Answers (9)

Syam Gopi Chand
Syam Gopi Chand

Reputation: 1

while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
{
    if (dropdownlist1.SelectedValue == textBox1.text)
    {
      // Add your logic here.
    }
}

//resetting to 0th index(optional)
dropdownlist1.SelectedIndex = 0;

Upvotes: 0

JB King
JB King

Reputation: 11910

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.

Upvotes: 27

Iyyappan
Iyyappan

Reputation: 191

You can simply do like this.

if (ddl.Items.FindByValue("value") != null) {
   ddl.SelectedValue = "value";
};

Upvotes: 3

ThrasHate
ThrasHate

Reputation: 11

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();
}

Upvotes: 1

Mike
Mike

Reputation: 21

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

Upvotes: 2

Drew McGhie
Drew McGhie

Reputation: 1086

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.

Upvotes: 7

Martin Brown
Martin Brown

Reputation: 25310

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;
                }
            }
        }

Upvotes: 0

tom.dietrich
tom.dietrich

Reputation: 8347

One line of code- split for readablilty.

this.DropDownList1.SelectedItem = this.DropDownList1.Items
     .SingleOrDefault(ddli => ddli.value == this.textbox1.value);

Upvotes: 0

Eldila
Eldila

Reputation: 15726

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;

Upvotes: 2

Related Questions