Export (0) Print
Expand All

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.

To implement search

  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 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.

Show:
© 2014 Microsoft