Add navigational buttons to a database application without using a BindingNavigator

On my previous post, we were able to add a navigational button to our application by using a BindingNavigator, today, we will do everything manually. To add a navigational button by hand, all you need to do is to bind your controls using this method and increment or decrement the Row index number to navigate around your records. This method is slightly similar to the methods used by most developers in adding navigational buttons to VB.net database applications.

For the sake of example, follow these steps(I assume here that you have already created a database file named “dbEmployee” and a table named “tblEmployee”):

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
open System.Data.OleDb
//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\Administrator\My Documents\dbEmployee.mdb")
 //creates an OleDbDataAdapter
let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)
let deletecommand=new System.Data.OleDb.OleDbCommand()

//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 Manually",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)
//creates our controls
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))  
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 topbutton=new Button(Text="Top", Location=new System.Drawing.Point(40, 170))
let bottombutton=new Button(Text="Bottom", Location=new System.Drawing.Point(120, 170))
//assings the font to our form
dataform.Font<-ffont
//adds the controls to our form
dataform.Controls.Add(exitbutton)
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(topbutton)
dataform.Controls.Add(bottombutton)

//binds the fieldnames to our label
emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))
fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))
lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))

//when the topbutton is clicked
//assigns 0 rowindexnumber value causing the 
//first record to be displayed
topbutton.Click.Add(fun top->
emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))
fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))
lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2)))

//when the bottombutton is clicked
//assign the index number of the last row as a rowindexnumer
//since we only have two records on our table
//so the index number of the last row is understood to be 1
//when manipulating large number of records, i suggest using
//dataset11.Tables.["tblEmployee"].Rows.Count() to
//automatically count the number of records

bottombutton.Click.Add(fun bottom->
emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(0))
fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(1))
lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(2)))

//when the exit button is clicked
exitbutton.Click.Add(fun exit->
//close the form and dataconnection
                    dataform.Close()
                    oleconn.Close())

//executes our application
dataform.Show()
Application.Run(dataform)

5. This will display the following output:



6. Simple...right?