DataTable Class in FSharp


Introduction:

A DataTable is a class of Disconnected architecture in the .NET Framework. It is found in the System.Data namespace and represents a single table. We can create a table and can add columns and rows of that table. Now, we will create an F# application and use the DataTable class.

Step 1: Create an F# application.

DataTable in f#

Step 2: First go-to Solution Explorer and right click on References.

DataTable in f#

Step 3: Click on Add References. Then a pop-up window with caption Add Reference will open:

DataTable 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.

DataTable in f#

Step 5: We write the following F# code on progrma.fs file. Here I am creating a table and adding columns as ID, F_NAME, L_NAME, SALARY. Look at the following code.

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

//----------- Creating Form and User Controls

let
frm = new Form(Height = 300, Width = 500)
let
dg = new DataGrid(Left = 20, Top = 20, Height = 200, Width = 350)
dg.BackColor <- Color.LightGray


//----------- Adding DataGrid

frm.Controls.Add(dg)


//----------- Creating DataTable Object as dt

let
dt = new DataTable()

//----------- Creating Different Object of DataColumn

let
dc1 = new DataColumn("ID")
let
dc2 = new DataColumn("F_NAME")
let
dc3 = new DataColumn("L_NAME")
let
dc4 = new DataColumn("SALARY")

//----------- Adding Columns to DataTable

dt.Columns.Add(dc1)
dt.Columns.Add(dc2)
dt.Columns.Add(dc3)
dt.Columns.Add(dc4)


//----------- DataBinding

dg.DataSource <-dt

Output:

DataTable in f#


Similar Articles