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

To add load, save, and cancel buttons to the BindingNavigator component

  1. 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 smart tag glyph (Smart Tag Glyph) 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 toLoadButton,SaveButton, andCancelButton, respectively.

    3. Set the Text property of the buttons toLoad, Save, andCancel.

    4. Set the DisplayStyle property for each of the buttons toText. Alternatively, you can set this property toImageorImageAndTextand 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 theLoadToolStripButton you created earlier and move this data-loading code into it.

    Your code should now look similar to the following:

    [Visual Basic]

    Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
        TableAdapterName.Fill(DataSetName.TableName)
    End Sub
    

    [C#]

    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.

    [Visual Basic]

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

    [C#]

    private void SaveButton_Click(System.Object sender, 
        System.EventArgs e)
    {
        TableAdapterName.Update(DataSetName.TableName);
    }
    

    Note

    In some cases, the BindingNavigator component will already have aSave button, but no code will have 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 theCancelToolStripButton you created earlier and write code to cancel any changes to the data record that is displayed.

    [Visual Basic]

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

    [C#]

    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

Reference

BindingSource Component Overview

BindingNavigator

BindingSource

ToolStrip

Other Resources

BindingNavigator Control (Windows Forms)