Can I remove multiple items from a DropDownList based on match to partial text

763 views Asked by At

I have a drop down list with contents similar to:

City1
City1_SearchCriteria
City2
City3
City3_SearchCriteria
City4
City4_SearchCriteria

I'm wondering if its possible to remove the items in this drop down that contain _SearchCriteria

Or should I be looking higher up at the DataSet that is the source of the drop down?

1

There are 1 answers

0
competent_tech On

I would personally fix the dataset. However, you can easily remove the offending items by working your way backward through the items list and removing the bad ones:

    For nI As Integer = cboMyData.Items.Count - 1 To 0 Step -1
        Dim oItem As ListItem
        oItem = cboMyData.Items(nI)
        If oItem.Text.EndsWith("_SearchCriteria", StringComparison.InvariantCultureIgnoreCase) Then
            cboDataType.Items.Remove(oItem)
        End If
    Next