Edit

Share via


How to: Add Load, Save, and Cancel Buttons to the Windows Forms BindingNavigator Control

The BindingNavigator control is a special-purpose ToolStrip control that is intended for navigating and manipulating controls on your form that are bound to data.

Because it's a ToolStrip control, the BindingNavigator component can be easily modified to include additional or alternative commands for the user.

In the following procedure, a TextBox control is bound to data, and the ToolStrip control that is added to the form is modified to include load, save, and cancel buttons.

Add load, save, and cancel buttons to the BindingNavigator component

  1. In Visual Studio, add a TextBox control to your form.

  2. Bind it to a BindingSource, which is bound to a data source. For this example, the BindingSource is bound to a database.

  3. After the dataset and table adapter are generated, drag a BindingNavigator control to the form.

  4. Set the BindingNavigator control's BindingSource property to the BindingSource on the form that is bound to the controls.

  5. Select the BindingNavigator control.

  6. Click the designer actions glyph (Small black arrow) so the BindingNavigator Tasks dialog appears and select Edit Items.

    The Items Collection Editor appears.

  7. In the Items Collection Editor, complete the following:

    1. Add a ToolStripSeparator and three ToolStripButton items by selecting the appropriate type of ToolStripItem and clicking the Add button.

    2. Set the Name property of the buttons to LoadButton, SaveButton, and CancelButton, respectively.

    3. Set the Text property of the buttons to Load, Save, and Cancel.

    4. Set the DisplayStyle property for each of the buttons to Text. Alternatively, you can set this property to Image or ImageAndText, and set the image to be displayed in the Image property.

    5. Click OK to close the dialog box. The buttons are added to the ToolStrip.

  8. Right-click the form and choose View Code.

  9. In the Code Editor, find the line of code that loads data into the table adapter. This code was generated when you set up the data binding in step 2. The code should be similar to the following: TableAdapterName.Fill(DataSetName.TableName). It will most likely be in the form's Load event.

  10. Create an event handler for the Click event of the Load ToolStripButton you created earlier and move this data-loading code into it.

    Your code should now look similar to the following:

    Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
        TableAdapterName.Fill(DataSetName.TableName)
    End Sub
    
    private void LoadButton_Click(System.Object sender,
        System.EventArgs e)
    {
        TableAdapterName.Fill(DataSetName.TableName);
    }
    
  11. Create an event handler for the Click event of the SaveToolStripButton you created earlier and write code to update the data within the table the control is bound to.

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        TableAdapterName.Update(DataSetName.TableName)
    End Sub
    
    private void SaveButton_Click(System.Object sender,
        System.EventArgs e)
    {
        TableAdapterName.Update(DataSetName.TableName);
    }
    

    Note

    In some cases, the BindingNavigator component already has a Save button, but no code has been generated by the Windows Forms Designer. In this case, you can place the preceding code in the Click event handler for that button, rather than creating an entirely new button on the ToolStrip. However, the button is disabled by default, so you must set the Enabled property of the button to true to have the button function correctly.

  12. Create an event handler for the Click event of the Cancel ToolStripButton you created earlier and write code to cancel any changes to the data record that is displayed.

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        BindingSourceName.CancelEdit()
    End Sub
    
    private void CancelButton_Click(System.Object sender, System.EventArgs e)
    {
        BindingSourceName.CancelEdit();
    }
    

    Note

    The CancelEdit method is scoped to the row of data. Save any changes you make while viewing that individual record before navigating to the next record.

See also