Export (0) Print
Expand All
3 out of 5 rated this helpful - Rate this topic

Navigating Data in Windows Forms

Visual Studio .NET 2003

In a Windows application, navigation through records in a data source is managed by the data-binding layer. The CurrencyManager object associated with a table or view in a dataset supports a Position property. The Position property is used by the data-binding mechanism to make sure that individual controls all read and write data to the same record.

Because the dataset can contain multiple data sources — multiple data tables or views — or because the controls on a form can be bound to two or more data lists, there can be multiple currency managers on the form, one for each source. To simplify data binding, Windows Forms expose a BindingContext object that provides a single point of access for all the form's CurrencyManager objects. The BindingContext object passes through the property values of individual currency managers, such as their Position property.

To navigate in a Windows Form

  • Set the Position property of the CurrencyManager for your bound data to the record position to go to. For information on adding a new record to your data set, see Inserting New Records into a Dataset.

    The following example illustrates the position of the CurrencyManager being incremented by 1 when the button nextButton is clicked. The CurrencyManager is associated with the authors table of a dataset dsPubs1.

    ' Visual Basic
    Private Sub nextButton_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles nextButton.Click
       Me.BindingContext(dsPubs1, "authors").Position += 1
    End Sub
    
    // C#
    private void nextButton_Click(object sender, System.EventArgs e)
    {
       this.BindingContext[dsPubs1, "authors"].Position += 1;
    }
    
    // C++
    private:
       void nextButton_Click(System::Object* sender, System::EventArgs* e)
       {
          this->BindingContext->get_Item(dsPubs1, S"authors")->Position = 
             this->BindingContext->get_Item(dsPubs1, S"authors")->Position 
             + 1;
       }
    
    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.

To check whether you have passed the end or beginning

  • Create an event handler for the CurrencyManager object's 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.

    ' Visual Basic
    Protected Sub Binding_PositionChanged(ByVal sender As _
    Object, ByVal e As EventArgs)
       If Me.BindingContext(dsPubs1, "authors").Position = Me.BindingContext(dsPubs1, "authors").Count - 1 Then
          nextButton.Enabled = False
       Else
          nextButton.Enabled = True
       End If
    End Sub
    
    // C#
    private void Binding_PositionChanged(object sender, System.EventArgs e)
    {
       if (this.BindingContext[dsPubs1, "authors"].Position == 
       (this.BindingContext[dsPubs1, "authors"].Count - 1))
          nextButton.Enabled = false;
       else
          nextButton.Enabled = true;
    }
    
    // C++
    private:
       void Binding_PositionChanged(System::Object* sender,
          System::EventArgs* e)
       {
          if (this->BindingContext->get_Item(dsPubs1, S"authors")->
              Position == (this->BindingContext->get_Item(dsPubs1,
              S"authors")->Count - 1))
             nextButton->Enabled = false;
          else
             nextButton->Enabled = true;
       }
    
    Note   Be aware that, should you change via code the list you are navigating, it will be important to re-enable the Next and Back buttons, so that users may browse the entire length of the new list. Additionally, be aware that the above PositionChanged event for the specific BindingContext object you are working with needs to be wired up using a delegate. The following is an example of a delegate for the
    Binding_PositionChanged
    code:
    ' Visual Basic
    AddHandler Me.BindingContext(dsPubs1, "authors").PositionChanged, AddressOf Me.Binding_PositionChanged
    
    // C#
    this.BindingContext[dsPubs1, "authors"].PositionChanged += new EventHandler(this.Binding_PositionChanged);
    
    // C++
    this->BindingContext->get_Item(dsPubs1, S"authors")->PositionChanged += 
       new System::EventHandler(this, Binding_PositionChanged);
    

See Also

Providers of Data to Windows Forms | Consumers of Data on Windows Forms | Data Binding and Windows Forms | Windows Forms Data Architecture | Data Form Wizard

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.