ARTICLE

DataBinding with a ComboBox in FSharp

Posted by A K Articles | F# October 31, 2011
In this article you will learn DataBinding with a ComboBox in F# in very simple way.
Reader Level:

Creating DataBase: We create a DataBase for getting records into a ComboBox. Here, in this example, the DataBase name is student_record, table name is student and Columns are Roll_No and Name. I have written code below for creating this database. 

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 with a ComboBox. We follow the following steps for this.

Step 1: Create an F# application.

step1.gif

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

step2.gif

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

databinding with combobox in f#

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

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

open System
open
System.Windows.Forms
open
System.Data.SqlClient
open
System.Data
 

//  cteating connection string

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

let
da = new SqlDataAdapter("select * from student",constring)
//creating DataSet

let
ds = new DataSet()
//filling the DataSet

da.Fill(ds)|>ignore

//creating Form

let
form = new Form()
//creating ComboBox

let
cmb = new ComboBox()
//Adding Control

form.Controls.Add(cmb)

//Binding ComboBox

cmb.DataSource <- ds.Tables.[0]
cmb.DisplayMember <- "Roll_No"// For displaying column values
 
Application.Run(form)

Now we run this application.

Output: 

databinding with combobox in f#

We Click on ComboBox. We look that it show all the Roll_No.

databinding with combobox in f#

In Student table, There are two columns. Suppose we want to select Roll_No of students into ComboBox and display student name in a Label or TextBox whose Roll_No is selected in the ComboBox. For this, We will Write code on SelectedValueChanged event of ComboBox. In this example, I am displaying record in Label. Write the following code.

open
System
open
System.Windows.Forms
open
System.Data.SqlClient
open
System.Data
//  cteating connection string

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

let
da = new SqlDataAdapter("select * from student",constring)
//creating DataSet

let
ds = new DataSet()
//filling the DataSet

da.Fill(ds)|>ignore

//creating Form

let
form = new Form()
//creating ComboBox

let
cmb = new ComboBox()
let
lbl = new Label(Top = 50)
//Adding Control

form.Controls.Add(cmb)
form.Controls.Add(lbl)

//Binding ComboBox

cmb.DataSource <- ds.Tables.[0]
cmb.DisplayMember <- "Roll_No"// For displaying column values
cmb.ValueMember <- "Name"// for containing value
cmb.DataSource <- ds.Tables.[0]
cmb.SelectedValueChanged.Add(fun a -> lbl.Text <- cmb.SelectedValue.ToString() )
 
Application.Run(form)

Now we run this application.

Output:

databinding with combobox in f#
 

COMMENT USING
Employers - Post Free Jobs