DataBinding with DataGridView in FSharp


Introduction: DataBinding is the most important thing when we develop any application. It is a process to map a value or set of values to a user interface control. We store data at back-end on database and transfer data to and from the front-end.

Creating DataBase: We create a DataBase for binding. Here, in my example, the DataBase name is student_record, table name is student and Columns are Roll_No and Name. I am using SQL server. I have written code for creating this database. Look at the below SQL statements.

create database student_record

use student_record

create table student
(   Roll_No int primary key,
    Name varchar(20) )

insert into student values(1,'Alok Pandey')
insert into student values(2,'Satish Kumar')
insert
into student values(3,'Amitabh Pandey')
insert
into student values(4,'Pramod Sharma')

insert
into student values(5,'Durgesh Pandey')

After creating the DataBase and table we start to a develop F# application for DataBinding. We follow the following steps for this.

Step 1: Take a F# application. Like the below figure

databinding in f#

Page will look like below figure.

databinding in f#

Step 2: We go to Solution Explorer and Right Click on References. Like below figure.

databinding in f#

Step 3: Click on Add References. Then a pop-up window with caption Add Reference will open as in the below figure.

databinding in f#

Step 4: Click at .Net on Add Reference window and select System.Windows.Forms, System.Drawing and System.Data with holding down Ctrl key and Click on Ok.

databinding in f#

Step 5: Write the below F# code in the Program.fs file.

//importing namespace
open
System
open
System.Windows.Forms
open
System.Drawing
open
System.Data.SqlClient
open
System.Data

//  cteating connection string

let
constring = @"Data Source=Server_Name;Initial Catalog=student_record;Integrated Security=True"

//creating DataAdapter with two parameter
//one for command string and
//another for connection string

let
adapter = new SqlDataAdapter("select * from student",constring)

//creating DataSet

let
ds = new DataSet()

//filling the DataSet

adapter.Fill(ds) |>ignore


//creating Form

let
form = new Form()

//creating DataGridView

let
gridview = new DataGridView()

//Adding DataGridView

form.Controls.Add(gridview)


//Binding DAtaGridView to DataSet

gridview.DataSource <- ds.Tables.[0]


//Showing the form

Application.Run(form)

Step 6: Run the code by pressing Ctrl+F5 key.

 databinding in f#

 
We note that all records are not visible in the DataGridView because the records requires more width of DataGridView and we did not set the width of DataGridView. Now we set if by adding new code as
let gridview = new DataGridView(Width=500, Height=500) in code. Modified code is given below.

//only let gridview = new DataGridView(Width=500, Height=500) is added in previous code

open
System
open
System.Windows.Forms
open
System.Drawing
open
System.Data.SqlClient
open
System.Data
let
constring = @"Data Source=server_name;Initial Catalog=student_record;Integrated Security=True"
let
adapter = new SqlDataAdapter("select * from student",constring)
let
ds = new DataSet()
adapter.Fill(ds) |>ignore

let
form = new Form()
let
gridview = new DataGridView(Width=500, Height=500)
form.Controls.Add(gridview)
gridview.DataSource <- ds.Tables.[0]
Application.Run(form)

Run the application.


databinding in f# 


Similar Articles