You can iterate over the elements in your list, and when you find the first one whose line of elements contains a template, you can set the Selected property to true.
bool found = false; int i = 0; while (!found && i<dropdownlist1.Items.Count) { if (dropdownlist1.Items.ToString().Contains("three")) found = true; else i++; } if(found) dropdownlist1.Items[i].Selected = true;
Or you can write a method (or extension method) that will do this for you
public bool SelectByPartOfTheValue(typeOfTheItem[] items, string part) { bool found = false; bool retVal = false; int i = 0; while (!found && i<dropdownlist1.Items.Count) { if (items.ToString().Contains("three")) found = true; else i++; } if(found) { items[i].Selected = true; retVal = true; } return retVal; }
and name it as follows
if(SelectByPartOfTheValue(dropdownlist1.Items, "three") MessageBox.Show("Succesfully selected"); else MessageBox.Show("There is no item that contains three");
source share