Compartir a través de


How to: Search Data in a DataRepeater Control (Visual Studio)

When you are using a DataRepeater control that contains many records, you may want to let users search for a specific record. Rather than searching the data in the control itself, you can implement a search by querying the underlying BindingSource. If the item is found, you can then use the CurrentItemIndex property to select the item and scroll it into view.

  1. Drag a TextBox control from the Toolbox onto the form that contains the DataRepeater control.

  2. In the Properties window, change the Name property to SearchTextBox.

  3. Drag a Button control from the Toolbox onto the form that contains the DataRepeater control.

  4. In the Properties window, change the Name property to SearchButton. Change the Text property to Search.

  5. Double-click the Button control to open the Code Editor, and add the following code to the SearchButton_Click event handler.

    Private Sub SearchButton_Click() Handles SearchButton.Click
        Dim foundIndex As Integer 
        Dim searchString As String
        searchString = SearchTextBox.Text
        foundIndex = ProductsBindingSource.Find("ProductID", 
           searchString)
        If foundIndex > -1 Then
            DataRepeater1.CurrentItemIndex = foundIndex
        Else
            MsgBox("Item " & searchString & " not found.")
        End If 
    End Sub
    
    private void searchButton_Click(System.Object sender, System.EventArgs e)
    {
        int foundIndex;
        string searchString;
        searchString = searchTextBox.Text;
        foundIndex = productsBindingSource.Find("ProductID", searchString);
        if (foundIndex > -1)
        {
            dataRepeater1.CurrentItemIndex = foundIndex;
        }
        else
        {
            MessageBox.Show("Item " + searchString + " not found.");
        }
    }
    

    Replace ProductsBindingSource with the name of the BindingSource for your DataRepeater, and replace ProductID with the name of the field that you want to search.

See Also

Tasks

Troubleshooting the DataRepeater Control (Visual Studio)

How to: Change the Appearance of a DataRepeater Control (Visual Studio)

Reference

DataRepeater

Concepts

Introduction to the DataRepeater Control (Visual Studio)