Data Binding in Windows Form Using F#

Introduction

This article explains data binding. The .NET Framework provides data binding in Windows Forms applications to access the data from a database such as SQL Server. In this case a control in the UI (User Interface) is bound to data values from a data source.

Data Binding

Data Binding is the process between a User Interface (UI) and a data source. When a binding is established and the data changes, the User Control bound to the data can display the changes automatically.

  • Simple Data Binding
  • Complex Data Binding

Simple Data Binding

In Simple Data Binding, a control is bound to a single data element from the data source. For example a TextBox control, Label control displays a single value.

Complex Data Binding

Complex Data Binding has the ability to bind more than one item on the control from the data source, for example a DataGrid, Listbox and so on controls.

Features of Data Binding

  • Better performance of the application.
  • Customization of default data binding process by modifying the generated code.
  • Fine control on data binding through events.
  • Reduction in code size.

Write the following procedure for creating a database and a table in SQL Server:

Create Database Employee

use Employee

create table EmployeeInfo

(

EmpId int identity(1,1),

Emp_Name varchar(max),

Emp_Department varchar(max)

)

Write the following procedure for inserting the values into the table columns:

insert into EmployeeInfo values('Pankaj','Web Developer')

 

Write the following query to execute the table schema:

 

select * from EmployeeInfo 

 

TableSchema

 

Now  I will show you how to bind the values in user controls from the data source. Use the following procedure to create the sample.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

figure-create application

Step 2:

Now go the Solution Explorer; on the right side of the application. Right-click on "References" and select "Add references".

SelectReferences

 


AddReferences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the Ctrl key and click on "Ok".

ImportNamespaces

Write the following code for binding the data values from the data source to the Windows Forms form.

Step 4:

open System  

open System.Windows.Forms  

open System.Data  

open System.Data.SqlClient

open System.Drawing    

let constring = @"Data Source=MCNDESKTOP34;Initial Catalog=Employee;User ID=; Password=" 

let con=new SqlConnection(constring)

let dataadpter = new SqlDataAdapter("Select * from EmployeeInfo", con)   

let ds = new DataSet()  

dataadpter.Fill(ds,"EmployeeInfo")|>ignore  

let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)  

let databindform = new Form(Text="DataBinding")    

let lblEmpid=new Label(Text="EmpId:",Top=20,Left=40,Width=60)  

let lblEmpname=new Label(Text="Emp Name:",Top=60,Left=0,Width=100)  

let lblEmpdept=new Label(Text="Emp Department:",Top=100,Left=0,Width=130)

let exitbutton=new Button(Text="Exit",Top=150,Left=70)  

let lblid=new Label(Top=20,Left=160,BorderStyle=BorderStyle.FixedSingle,Width=40)  

let lblname=new Label(Top=60,Left=140,BorderStyle=BorderStyle.FixedSingle,Width=120)  

let lbldept=new Label(Top=100,Left=140,BorderStyle=BorderStyle.FixedSingle,Width=120)  

databindform.Font<-ffont  

databindform.Controls.Add(exitbutton)  

databindform.Controls.Add(lblEmpid)  

databindform.Controls.Add(lblEmpname)  

databindform.Controls.Add(lblEmpdept)  

databindform.Controls.Add(lblid)  

databindform.Controls.Add(lblname)  

databindform.Controls.Add(lbldept)  

lblid.DataBindings.Add(new Binding("Text",ds,"EmployeeInfo.Empid"))  

lblname.DataBindings.Add(new Binding("Text",ds,"EmployeeInfo.Emp_Name"))  

lbldept.DataBindings.Add(new Binding("Text",ds,"EmployeeInfo.Emp_Department"))  

exitbutton.Click.Add(fun exit->  

                    databindform.Close()  

                    con.Close())  

databindform.Show()  

Application.Run(databindform)  

Step 5

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:

AfterDebug

Summary

The .NET Framework provides data binding in Windows Forms to fetch data from a database such as SQL Server.  The Data Binding allows users to bind the values in read-only selection lists.


Similar Articles