26

In C#, I have variable, a, of type string.

How do I find item by value of a in combobox (I want find item with value no display text of combobox).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duy Khanh
  • 384
  • 1
  • 5
  • 11

5 Answers5

40

You can find it by using the following code.

int index = comboBox1.Items.IndexOf(a);

To get the item itself, write:

comboBox1.Items[index];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
st mnmn
  • 3,555
  • 3
  • 25
  • 32
16

You should see a method on the combo box control for FindStringExact(), which will search the displaymember and return the index of that item if found. If not found will return -1.

//to select the item if found: 
mycombobox.SelectedIndex = mycombobox.FindStringExact("Combo"); 

//to test if the item exists: 
int i = mycombobox.FindStringExact("Combo"); 
if(i >= 0)
{
  //exists
}
Andrew Mulvaine
  • 161
  • 1
  • 2
  • 2
    I think FindExactString() works on the DisplayMember property of the ComboBox. I think the question is asking about how to match on the ValueMember property of a ComboBox. – andyabel Dec 18 '17 at 22:00
0

I know my solution is very simple and funny, but before I train I used it. Important: DropDownStyle of combobox must be "DropDownList"!

First in combobox and then:

bool foundit = false;
String mystr = "item_1";
mycombobox.Text = mystr;
if (mycombobox.SelectedText == mystr) // Or using mycombobox.Text
    foundit = true;
else foundit = false;

It works for me right and resolved my problem... But the way (solution) from @st-mnmn is better and fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3290286
  • 687
  • 1
  • 7
  • 8
0

Hi Guys the best way if searching for a text or value is

int Selected = -1;    
int count = ComboBox1.Items.Count;
    for (int i = 0; (i<= (count - 1)); i++) 
     {        
         ComboBox1.SelectedIndex = i;
        if ((string)(ComboBox1.SelectedValue) == "SearchValue") 
        {
            Selected = i;
            break;
        }

    }

    ComboBox1.SelectedIndex = Selected;
Larry Z
  • 23
  • 4
Teezy7
  • 515
  • 5
  • 9
  • Use "if ((string) (ComboBox1.Items[i]) == "SearchValue")" instead of "ComboBox1.SelectedIndex = i; if ((string)(ComboBox1.SelectedValue) == "SearchValue")" – amirfg May 12 '20 at 13:18
0

Sorry for VB.NET code, please convert it to c#.

I prepared the following function bcoz of .NET 3.5 Windows Application:

Public Function FindIndex_by_value(ByRef combo As ComboBox, ByVal value As String) As Integer

Dim idx As Integer

For i As Integer = 0 To combo.Items.Count - 1
  Dim itm As DataRowView

  itm = combo.Items(i)
  Dim vl As String = itm.Item(0) 
  If vl = value Then
    idx = i
    Exit For
  End If
Next

Return idx
  End Function

using this function:

Dim idx As Integer = FindIndex_by_value(comboBox1, "Value_to_Search")
comboBox1.SelectedIndex = idx

Important: here 0 is index of the value column and I used the first column as value column in my DB query.

Imran Rafique
  • 329
  • 3
  • 10