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

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
Search for a command to run...

Pieter van der Westhuizen, a professional freelance web & mobile developer and founder of Coalition Software.
A recent requirement came up for configurable dynamic forms in a mobile application. There are a few dynamic form packages for Flutter, but I wanted a bit more control over the schema and behavior of the form. Here follows my attempt at creating dyna...

If you've ever written a mobile app with Flutter that integrates with an API, then you'll know that there are a lot of model objects that get passed between the app and the API. JSON Serialization in Flutter can also take a fair amount of boilerplate...

Back in May 2020, Microsoft announced a new feature for Azure Blob Storage called Blob Index. Essentially, this enables you to add key/value tags to your Blob objects and be able to query said Blob objects without having to use a separate service lik...

In my last post, "Using AWS Cognito with Xamarin Forms", I showed how to authenticate with Amazon Cognito using Xamarin Forms and the Xamarin.Essentials Web Authenticator. In this post, we'll go through the process of using the AWS Cognito Hosted UI ...

In this post, I'll show you how to quickly and easily set up user authentication for your Xamarin Forms app using Amazon Cognito. AWS Cognito is a user identity management solution by Amazon. It is a really easy way to add authentication to your appl...

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…