如何以编程方式搜索 C# DropDownList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:54:10  来源:igfitidea点击:

How to Search Through a C# DropDownList Programmatically

c#searchdrop-down-menu

提问by

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”语句,这些语句在不同的下拉列表中搜索在文本框中输入的特定值。我能够编写在每个下拉列表中找到特定值的代码;但是,在这发生之前,我需要添加一个“if”语句,说“如果下拉列表不包含特定值,则转到下一个 if 语句,依此类推”。以下是我到目前为止所拥有的示例:

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?

这样做是根据我在 textbox1 中的条目读取或扫描每个下拉列表中的第一个值或索引。不幸的是,它只标识第一个值或索引。我需要弄清楚如何扫描整个下拉列表中每个“if”语句的所有值,以找到匹配的 textbox1 值。有没有人有什么建议?

Thank you,

谢谢,

DFM

东风

采纳答案by JB King

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.

这是我关于如何查看值是否在下拉列表中的建议。

回答by Eldila

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

我会制作一个下拉框列表,然后使用 linq 对其进行选择。

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;

回答by Drew McGhie

The DropDownListinherits the Itemscollection from the ListControl. Since Items is an Array, you can use this syntax:

DropDownList的继承项目从集合列表控件。由于 Items 是一个数组,您可以使用以下语法:

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

dropdownlist1.Items.Contains(textbox1.Text) 作为布尔值。

回答by tom.dietrich

One line of code- split for readablilty.

一行代码拆分以提高可读性。

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

回答by Martin Brown

If you don't want to use LINQ:

如果您不想使用 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;
                }
            }
        }

回答by Mike

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

回答by ThrasHate

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

回答by Iyyappan

You can simply do like this.

你可以简单地这样做。

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