CSLA.Net:Step-by-Step – Using the BindingNavigator

I was recently asked how to use the BindingNavigator to scroll through a list of objects. It is actually a very simple process, and here’s how:
I’m using the same Northwind BO Library as with the previous posts. We have a Customer object, and the table design looks something like this:

Right, next create a new class called Customers. This class will inherit from EditableRootListBase . You can paste the following into the class:
_ Public Class Customers Inherits EditableRootListBase(Of Customer)
#Region " Business Methods " Protected Overrides Function AddNewCore() As Object Dim item As Customer = Customer.NewCustomer Add(item) Return item End Function
#End Region
#Region " Factory Methods " Public Shared Function GetList() As Customers Return DataPortal.Fetch(Of Customers)() End Function Private Sub New() Me.AllowEdit = True Me.AllowNew = True Me.AllowRemove = True End Sub
#End Region
#Region " Data Access " Private Overloads Sub DataPortal_Fetch() Me.RaiseListChangedEvents = False Using sqlconn As New SqlConnection(Database.NorthwindConnection) sqlconn.Open() Using cm As SqlCommand = sqlconn.CreateCommand cm.CommandType = CommandType.Text cm.CommandText = "SELECT * FROM Customers WITH (NOLOCK)" Using dr As New SafeDataReader(cm.ExecuteReader()) While dr.Read Add(Customer.GetCustomer(dr.GetInt32("CustomerID"))) End While dr.Close() End Using End Using sqlconn.Close() End Using Me.RaiseListChangedEvents = True End Sub
#End Region End Class
Compile your project and add a new form. Drag the Customers object from the Data Sources window to your newly created form. It should automatically add a BindingNavigator and a Datagridview to your form. Go ahead and delete the grid.
Next, drag the CustomerTextID, CompanyName, ContactName fields onto your form. Your form should look something like this:

Select the Save button on the BindingNavigator and set it’s Enabled property to True. Switch to Code view and add the following declaration:
Dim _cust As Customer
Next, add the following code to your form’s Load Event:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load CustomersBindingSource.DataSource = Customers.GetList End Sub
Now, we need to handle the BindingNavigator’s Save event. You do not need to worry about the AddNew or Delete Events, the business object handles those for you.
Add the following code to the BindingSource’s PositionChanged event:
Private Sub CustomersBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles CustomersBindingSource.PositionChanged _cust = CType(CustomersBindingSource.Current, Customer) End Sub
Finally, add the following to the Save button’s click event:
Private Sub CustomersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles CustomersBindingNavigatorSaveItem.Click _cust.ApplyEdit() _cust.Save(True) End Sub
Press F5 and see your handy work, it should look something like this:

There you go, a quick and neat way to use the BindingNavigator with CSLA objects. Until next time, keep coding…






