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:
  1. let bindingsourceobjname=new BindingSource()  
for instance:
  1. 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:
  1. let bindingnavigatorobjvariable=new BindingNavigator()  
for instance:
  1. 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 “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the location of the class files  
  3. //that will be needed in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a connection object  
  9. let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  10.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  11. //adds an oleDbDataAdapter  
  12. let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)  
  13. //generate a dataset  
  14. let dataset11 = new DataSet()  
  15. //fill our dataset with record values  
  16. dataadapter.Fill(dataset11,"tblEmployee")|>ignore  
  17. //creates a form  
  18. let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))  
  19. //creates a datagrid  
  20. let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))  
  21. //creates a binding source to simplify the binding process  
  22. let bindingsource=new BindingSource()  
  23. //creates a binding navigator  
  24. //this will allow us to add navigational buttons to our data grid  
  25. let bindingnav=new BindingNavigator()  
  26. //creates a toolstrip buttons for our binding navigator  
  27. let movefirst=new ToolStripButton(Text="Top")  
  28. let moveprev=new ToolStripButton(Text="Prev")  
  29. let movenext=new ToolStripButton(Text="Next")  
  30. let movelast=new ToolStripButton(Text="Bottom")  
  31. //adds the toolstripbuttons to our binding navigator  
  32. bindingnav.Items.Add(movefirst)|>ignore  
  33. bindingnav.Items.Add(moveprev)|>ignore  
  34. bindingnav.Items.Add(movenext)|>ignore  
  35. bindingnav.Items.Add(movelast)|>ignore  
  36. //adds a function to each buttons  
  37. bindingnav.MoveFirstItem<-movefirst  
  38. bindingnav.MoveNextItem<-movenext  
  39. bindingnav.MovePreviousItem<-moveprev  
  40. bindingnav.MoveLastItem<-movelast  
  41. //assigns the dataset name as a bindingsource datasource  
  42. bindingsource.DataSource<-dataset11  
  43. //assigns our table as a binding source datamember  
  44. bindingsource.DataMember<-"tblEmployee"  
  45. //assigns the bindingsource name as a binding navigators  
  46. //bindingsource value  
  47. bindingnav.BindingSource<-bindingsource  
  48. //opens the connection  
  49. olecon.Open()  
  50. //adds the controls to our form  
  51. dataform.Controls.Add(bindingnav)  
  52. datagrid.DataSource <- bindingsource  
  53.   
  54. //add the datagrid to our form  
  55. dataform.Controls.Add(datagrid)  
  56. //execute our application  
  57. dataform.Show()  
  58. 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.

Changing the DataGridView Header Text in Visual F#

To change the header text of a grid contrl in Visual F#, the first thing that you’ll need to do is to create a grid view columns. This can be done by using the following syntax:
  1. let columnname=new DataGridViewTextBoxColumn()  
For instance:
  1. let chrempnocol=new DataGridViewTextBoxColumn()  
After that, you’ll need to add that column to the data grid. This can be done by using the following syntax:
  1. dataobjvariable.Columns.Add(columnname)  
For example:
  1. datagrid.Columns.Add(chrempnocol)  
Then finally, you’ll need to bind the column name to the fieldname where you will be getting the record value followed by changing its header text using the DataPropertyName and HeaderText propeties.

For a simple example on changing the header text of a grid control, follow these steps:

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 “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the location of the class files  
  3. //that will be needed in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a connection object  
  9. let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  10.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  11. //adds an oleDbDataAdapter  
  12. let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)  
  13. //generate a dataset  
  14. let dataset11 = new DataSet()  
  15. //fill our dataset with record values  
  16. dataadapter.Fill(dataset11,"tblEmployee")|>ignore  
  17. //creates a form  
  18. let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))  
  19. //creates a datagrid  
  20. let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))  
  21. //creates a grid control colums  
  22. let chrempnocol=new DataGridViewTextBoxColumn()  
  23. let chrfnamecol=new DataGridViewTextBoxColumn()  
  24. let chrlnamecol=new DataGridViewTextBoxColumn()  
  25. //adds the columns into our datagrid  
  26. datagrid.Columns.Add(chrempnocol)|>ignore  
  27. datagrid.Columns.Add(chrfnamecol)|>ignore  
  28. datagrid.Columns.Add(chrlnamecol)|>ignore  
  29. //opens the database connection and  
  30. //set our table as the datagrid's datasource  
  31. olecon.Open()  
  32. datagrid.DataSource <- dataset11.Tables.["tblEmployee"]  
  33. //links our fieldname to each grid  
  34. //and change its header text  
  35. chrempnocol.DataPropertyName<-"chrempno"  
  36. chrempnocol.HeaderText<-"Employee No."  
  37. chrfnamecol.DataPropertyName<-"chrfname"  
  38. chrfnamecol.HeaderText<-"First Name"  
  39. chrlnamecol.DataPropertyName<-"chrlname"  
  40. chrlnamecol.HeaderText<-"Last Name"  
  41. //add the datagrid to our form  
  42. dataform.Controls.Add(datagrid)  
  43. //execute our application  
  44. dataform.Show()  
  45. 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.That's all for now:)