Share via


How to: Navigate Data in Windows Forms

In a Windows application, the easiest way to navigate through records in a data source is to bind a BindingSource component to the data source and then bind controls to the BindingSource. You can then use the built-in navigation method on the BindingSource such a MoveNext, MoveLast, MovePrevious and MoveFirst. Using these methods will adjust the Position and Current properties of the BindingSource appropriately. You can also find an item and set it as the current item by setting the Position property.

To increment the position in a data source

  • Set the Position property of the BindingSource for your bound data to the record position to go to. The following example illustrates using the MoveNext method of the BindingSource to increment the Position property when the nextButton is clicked. The BindingSource is associated with the Customers table of a dataset Northwind.

    Note

    Setting the Position property to a value beyond the first or last record does not result in an error, as the .NET Framework will not allow you to set the position to a value outside the bounds of the list. If it is important in your application to know whether you have gone past the first or last record, include logic to test whether you will exceed the data element count.

    Private Sub nextButton_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles nextButton.Click
        Me.customersBindingSource.MoveNext()
    End Sub
    
    private void nextButton_Click(object sender, System.EventArgs e)
    {
        this.customersBindingSource.MoveNext();
    }
    

To check whether you have passed the end or beginning

  • Create an event handler for the PositionChanged event. In the handler, you can test whether the proposed position value has exceeded the actual data element count.

    The following example illustrates how you can test whether you have reached the last data element. In the example, if you are at the last element, the Next button on the form is disabled.

    Note

    Be aware that, should you change the list you are navigating in code, you should re-enable the Next button, so that users may browse the entire length of the new list. Additionally, be aware that the above PositionChanged event for the specific BindingSource you are working with needs to be associated with its event-handling method. The following is an example of a method for handling the PositionChanged event:

    Sub customersBindingSource_PositionChanged(ByVal sender As Object, _
        ByVal e As EventArgs)
    
        If customersBindingSource.Position = _
            customersBindingSource.Count - 1 Then
            nextButton.Enabled = False 
        Else
            nextButton.Enabled = True 
        End If 
    End Sub
    
    void customersBindingSource_PositionChanged(object sender, EventArgs e)
    {
        if (customersBindingSource.Position == customersBindingSource.Count - 1)
            nextButton.Enabled = false;
        else
            nextButton.Enabled = true;
    }
    

To find an item and set it as the current item

  • Find the record you wish to set as the current item. You can do this using the Find method of the BindingSource, if your data source implements IBindingList. Some examples of data sources that implement IBindingList are BindingList<T> and DataView.

    Sub findButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles findButton.Click
        Dim foundIndex As Integer = customersBindingSource.Find("CustomerID", _
            "ANTON")
        customersBindingSource.Position = foundIndex
    End Sub
    
    void findButton_Click(object sender, EventArgs e)
    {
        int foundIndex = customersBindingSource.Find("CustomerID", "ANTON");
        customersBindingSource.Position = foundIndex;
    }
    

See Also

Concepts

Data Sources Supported by Windows Forms

Change Notification in Windows Forms Data Binding

Data Binding and Windows Forms

Other Resources

Windows Forms Data Binding