mike mcintyre's

.N e t J o u r n a l

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

DecJanuary 2007Feb
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Archives

Topics

Source Code

Wednesday, January 31, 2007 #

Source Code: DAL For MS Access Databases - Part 6

DAL For MS Access Databases - Part 6

Previous parts of this article can be read at:  Part 1, Part 2, Part 3, Part 4, Part 5

Part five of this article demonstrated how to 1) create a custom BindingSource class; 2) bind a custom BindingSource class to a DataGridView; 3) extend a TableAdapter by adding queries to it; and 4) modify and/or extend a TableAdapter via its partial class.

Part six provides demonstrates how to use a BindingNavigator class with the custom AjaxCustomer BindingSource created in part five and provides a diagram of the major components of the DataService project to-date and the Windows Forms project that consumes services from the DataService.

Windows Forms Example Screen Shot

Use a BindingNavigator with the Custom AjaxCustomer BindingSource

A new form, the ExampleTwoForm, has been added to the Windows Forms example application to demonstrate how use a BindingNavigator control with the custom AjaxCustomer BindingSource that was created in an earlier part of this article.

You can use the .NET Windows Forms BindingNavigator control to create a standardized means for users to search and change data on a Windows Form. You frequently use BindingNavigator with the BindingSource component to enable users to move through data records on a form and interact with the records. To learn more about the BindingNavigator control click -> BindingNavigator Control Overview

Here are the steps taken to add a BindingNavigator to ExampleTwoForm to get basic navigation features for the Ajax Customer records:

A new form, the ExampleTwoForm, was added to the source code.

A new menu item to show ExampleTwoForm was added to the MainForm's Examples menu.

ExampleTwoForm was opened in the Visual Studio form designer.

In the Data Sources panel of Visual Studio, the drop down menu next to the AjaxCustomer data source was used to select 'Details'.

The AjaxCustomer data source was drug from the Data Sources panel to ExampleTwoForm's design surface.  Visual Studio automatically added: 1)a BindingSource component; 2) a BindingNavigator component; 3) a BindingNavigator control; and 4) labels and controls for editing Customer records.

The BindingNavigator control's AddNewItem and DeleteItem properties were set to (none):

The AddNewItem property was set to (none) to prevent the BindingNavigator from automatically adding a new customer row when it's add new button (the button with the + sign) is clicked. Instead our code will handle the click and call the DAL's AjaxCustomerAddNew method to add a new customer row.

The DeleteItem property was set to (none) to prevent the BindingNavigator from automatically deleting the current customer when it's delete button (the button with the X) is clicked. Instead our code will handle the click and call the DAL's AjaxCustomerDelete method to delete the current customer row.

Code was added to ExampleTwoForm to load all customers when the form is shown and to wire up the BindingNavigator control's Add New, Delete, and Save buttons.

 

Imports System.Data

Public Class ExampleTwoForm

 

    Private Sub ExampleTwoForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Make this form a MDI child of MainForm.

        Me.MdiParent = My.Forms.MainForm

    End Sub

 

    ' This code is called the BindingNavigator control's New button is clicked.

    ' Note: To prevent the BindingNavigator from automatically adding a new row

    '  when the New button is clicked, you must set the BindingNavigator's AddNewItem

    '  property to 'None'.

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

        ' Call the DAL's AjaxCustomerAddNew method.

        My.Application.DAL.AjaxCustomerAddNew()

    End Sub

 

    ' This code activates the BindingNavigator's Save button when any column

    ' of the Customer record is modified.

    Private Sub CustomerRecordModified(ByVal sender As Object, ByVal e As System.EventArgs) Handles _

    CityTextBox.Validated, LastOrderDateDateTimePicker.Validated, NameTextBox.Validated, _

    StateTextBox.Validated, StatusTextBox.Validated, StreetAddress1TextBox.Validated, _

    StreetAddress2TextBox.Validated, ZipTextBox.Validated

        ' If somethings been modified let the user save it.

        Me.AjaxCustomerBindingNavigatorSaveItem.Enabled = True

    End Sub

 

    ' This code is called the BindingNavigator control's Delete button is clicked.

    ' Note: To prevent the BindingNavigator from automatically deleting the current row

    '  when the Delete button is clicked, you must set the BindingNavigator's DeleteItem

    '  property to 'None'.

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click

 

        ' Cast the AjaxCustomerBindingSource's current object to

        '   an AjaxDataSet CustomerRow.

        Dim currentCustomerRow As DataService.AjaxDataSet.CustomerRow

        currentCustomerRow = CType(CType(Me.AjaxCustomerBindingSource.Current, DataRowView).Row, DataService.AjaxDataSet.CustomerRow)

 

        ' Verify the user want's to delete the current Customer record.

        If MessageBox.Show("Really delete " & currentCustomerRow.Name & "'s customer record?", "Delete Customer Record", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then

            ' Call the DAL's AjaxCustomerDelete method.

            My.Application.DAL.AjaxCustomerDelete(currentCustomerRow)

            ' Enable BindingNavigator's Save button.

            Me.AjaxCustomerBindingNavigatorSaveItem.Enabled = True

        Else

            ' Do nothing.

        End If

    End Sub

 

    ' This code is called when the BindingNavigator control's Save button is clicked.

    Private Sub AjaxCustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles AjaxCustomerBindingNavigatorSaveItem.Click

 

        Me.AjaxCustomerBindingSource.EndEdit()

 

        ' Use the DAL's AjaxCustomerUpdate method to save the Customer records (update the database).

        My.Application.DAL.AjaxCustomerUpdate()

 

        ' Disable the BindingNavigator control's Save button.

        Me.AjaxCustomerBindingNavigatorSaveItem.Enabled = False

 

    End Sub

End Class

Diagram

The diagram below presents a birds eye view of the major components of the DataService project and the Windows Forms project that references it.

When the DataService project is compiled the DataService.dll file is created. When the GetDotNetCodeFreeVisualBasicExample project gets compiled the GetDotNetCodeFreeVisualBasicExample.exe is created.

For more information:
 
BindingNavigator Class
 

mike mcintyre http://www.getdotnetcode.com

 

posted @ 10:19 AM