Adding navigational Buttons to your database application

The fastest way to add navigational buttons to your database application is to use a BindingNavigator. If you are not yet familiar with Binding Navigator, I suggest reading this post first before proceeding to the steps below:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System
open System.Windows.Forms
open System.Data
open System.Drawing
//creates a font
let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)  
//creates a connection object
let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
  Data Source=C:\Documents and Settings\station 2\My Documents\dbEmployee.mdb")
 //creates an OleDbDataAdapter
let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)
//generates a dataset
let dataset11 = new DataSet()
//fills the dataset with recod values
dataadpter.Fill(dataset11,"tblEmployee")|>ignore
//creates a form
let dataform = new Form(Text="Add Navigational Buttons",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)
//creates our controls  
let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)
let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)
let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)
let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)
let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)
let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)
let bindingsource=new BindingSource()
//creates a binding navigator
//this will allow us to add navigational buttons to our data grid
let bindingnav=new BindingNavigator(Dock=DockStyle.None,Location=new System.Drawing.Point(100, 170))
//creates a toolstrip buttons for our binding navigator
let movefirst=new ToolStripButton(Text="Top")
let moveprev=new ToolStripButton(Text="Prev")
let movenext=new ToolStripButton(Text="Next")
let movelast=new ToolStripButton(Text="Bottom")
let exitbutton=new ToolStripButton(Text="Exit")
//adds the toolstripbuttons to our binding navigator
bindingnav.Items.Add(movefirst)|>ignore
bindingnav.Items.Add(moveprev)|>ignore
bindingnav.Items.Add(movenext)|>ignore
bindingnav.Items.Add(movelast)|>ignore
bindingnav.Items.Add(exitbutton)|>ignore
//adds a function to each buttons
bindingnav.MoveFirstItem<-movefirst
bindingnav.MoveNextItem<-movenext
bindingnav.MovePreviousItem<-moveprev
bindingnav.MoveLastItem<-movelast
exitbutton.Click.Add(fun exit->
//close the form and dataconnection
                    dataform.Close()
                    oleconn.Close())
//assigns the dataset name as a bindingsource datasource
bindingsource.DataSource<-dataset11
//assigns our table as a binding source datamember
bindingsource.DataMember<-"tblEmployee"
//assigns the bindingsource name as a binding navigators
//bindingsource value
bindingnav.BindingSource<-bindingsource
//opens the connection
oleconn.Open()
//assings the font to our form
dataform.Font<-ffont
//adds the controls to our form
dataform.Controls.Add(label1)
dataform.Controls.Add(label2)
dataform.Controls.Add(label3)
dataform.Controls.Add(emplabel)
dataform.Controls.Add(fnamelabel)
dataform.Controls.Add(lnamelabel)
dataform.Controls.Add(bindingnav)
//binds the fieldnames to our label
emplabel.DataBindings.Add(new Binding("Text",bindingsource,"chrempno"))
fnamelabel.DataBindings.Add(new Binding("Text",bindingsource,"chrfname"))
lnamelabel.DataBindings.Add(new Binding("Text",bindingsource,"chrlname"))
//executes our application
dataform.Show()
Application.Run(dataform)
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.

BindingSource and BindingNavigator(OleDbDataAdapter Visual F#)

BindingSource allows us to simplify the binding process to our control. To create a binding source in Visual F#, use the following syntax:
let bindingsourceobjname=new BindingSource()
for instance:
let bindingsource=new BindingSource()
The two most important properties of BindingSource component are DataSource which specifies the data source of the bindingsource, and the Datamember which specifies the specific table to which your bindingsource is bound to.

BindingNavigator are simply used to add navigational buttons to your database application. The syntax in creating a binding source is:
let bindingnavigatorobjvariable=new BindingNavigator()
for instance:
let bindingnav=new BindingNavigator()
One of the essential properties of the BindingNavigator component is the BindingSource which specifies the bindingsource name to which the BindingNavigator is bound to.

Follow these steps for a simple example on using binding navigator:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

// Learn more about F# at http://fsharp.net
//specifies the location of the class files
//that will be needed in our application
open System
open System.Windows.Forms
open System.Data
open System.Drawing
//creates a connection object
let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
  Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")
//adds an oleDbDataAdapter
let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)
//generate a dataset
let dataset11 = new DataSet()
//fill our dataset with record values
dataadapter.Fill(dataset11,"tblEmployee")|>ignore
//creates a form
let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))
//creates a datagrid
let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))
//creates a binding source to simplify the binding process
let bindingsource=new BindingSource()
//creates a binding navigator
//this will allow us to add navigational buttons to our data grid
let bindingnav=new BindingNavigator()
//creates a toolstrip buttons for our binding navigator
let movefirst=new ToolStripButton(Text="Top")
let moveprev=new ToolStripButton(Text="Prev")
let movenext=new ToolStripButton(Text="Next")
let movelast=new ToolStripButton(Text="Bottom")
//adds the toolstripbuttons to our binding navigator
bindingnav.Items.Add(movefirst)|>ignore
bindingnav.Items.Add(moveprev)|>ignore
bindingnav.Items.Add(movenext)|>ignore
bindingnav.Items.Add(movelast)|>ignore
//adds a function to each buttons
bindingnav.MoveFirstItem<-movefirst
bindingnav.MoveNextItem<-movenext
bindingnav.MovePreviousItem<-moveprev
bindingnav.MoveLastItem<-movelast
//assigns the dataset name as a bindingsource datasource
bindingsource.DataSource<-dataset11
//assigns our table as a binding source datamember
bindingsource.DataMember<-"tblEmployee"
//assigns the bindingsource name as a binding navigators
//bindingsource value
bindingnav.BindingSource<-bindingsource
//opens the connection
olecon.Open()
//adds the controls to our form
dataform.Controls.Add(bindingnav)
datagrid.DataSource <- bindingsource

//add the datagrid to our form
dataform.Controls.Add(datagrid)
//execute our application
dataform.Show()
Application.Run(dataform)
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.