Yet another way of binding a control to a field name in Visual F#

On my last post, I’ve shown you how to bind a record value to a control using the DataBindings method. Another way of binding a control to a fieldname is by using Text property of a control and assigning the dataset collection properties as its value:

Syntax:

  1. controlobjvariable.Text<- (datasetname.Tables.["tablename"].Rows.Item(rowindexnumber).Item(columnindexnumber))  
For instance:
  1. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
The rowindexnumber of the first record on your table is 0, the second record is 1 and so on. Same goes with the columnindexnumber. For a simple example of application that uses the binding method above, 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 “:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory 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. open System.Data.OleDb  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. //creates a connection object  
  12. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  13.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  14.  //creates an OleDbDataAdapter  
  15. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  16. let deletecommand=new System.Data.OleDb.OleDbCommand()  
  17.   
  18. //generates a dataset  
  19. let dataset11 = new DataSet()  
  20. //fills the dataset with recod values  
  21. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  22. //creates a form  
  23. let dataform = new Form(Text="Bind Form Control Part 2",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  24. //creates our controls  
  25. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  26. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  27. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  28. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  29. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  30. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  31. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  32. //assings the font to our form  
  33. dataform.Font<-ffont  
  34. //adds the controls to our form  
  35. dataform.Controls.Add(exitbutton)  
  36. dataform.Controls.Add(label1)  
  37. dataform.Controls.Add(label2)  
  38. dataform.Controls.Add(label3)  
  39. dataform.Controls.Add(emplabel)  
  40. dataform.Controls.Add(fnamelabel)  
  41. dataform.Controls.Add(lnamelabel)  
  42.   
  43. //binds the fieldnames to our label  
  44. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  45. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  46. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))  
  47.   
  48. //when the exit button is clicked  
  49. exitbutton.Click.Add(fun exit->  
  50. //close the form and dataconnection  
  51.                     dataform.Close()  
  52.                     oleconn.Close())  
  53.   
  54. //executes our application  
  55. dataform.Show()  
  56. Application.Run(dataform)  
5. This will display the following output: