Changing the DataGridView Header Text in Visual F#

Posted by Rey Dacoco in

To change the header text of a grid contrl in Visual F#, the first thing that you’ll need to do is to create a grid view columns. This can be done by using the following syntax:

let columnname=new DataGridViewTextBoxColumn()
For instance:
let chrempnocol=new DataGridViewTextBoxColumn()
After that, you’ll need to add that column to the data grid. This can be done by using the following syntax:
dataobjvariable.Columns.Add(columnname)
For example:
datagrid.Columns.Add(chrempnocol)
Then finally, you’ll need to bind the column name to the fieldname where you will be getting the record value followed by changing its header text using the DataPropertyName and HeaderText propeties.

For a simple example on changing the header text of a grid control, follow these steps:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


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 “:

// Learn more about F# at http://fsharp.net
//specifies the location of the class files
//that will be needed in our application
open System
open System.Windows.Forms
open System.Data
open System.Drawing
//creates a connection object
let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
  Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")
//adds an oleDbDataAdapter
let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)
//generate a dataset
let dataset11 = new DataSet()
//fill our dataset with record values
dataadapter.Fill(dataset11,"tblEmployee")|>ignore
//creates a form
let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))
//creates a datagrid
let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))
//creates a grid control colums
let chrempnocol=new DataGridViewTextBoxColumn()
let chrfnamecol=new DataGridViewTextBoxColumn()
let chrlnamecol=new DataGridViewTextBoxColumn()
//adds the columns into our datagrid
datagrid.Columns.Add(chrempnocol)|>ignore
datagrid.Columns.Add(chrfnamecol)|>ignore
datagrid.Columns.Add(chrlnamecol)|>ignore
//opens the database connection and
//set our table as the datagrid's datasource
olecon.Open()
datagrid.DataSource <- dataset11.Tables.["tblEmployee"]
//links our fieldname to each grid
//and change its header text
chrempnocol.DataPropertyName<-"chrempno"
chrempnocol.HeaderText<-"Employee No."
chrfnamecol.DataPropertyName<-"chrfname"
chrfnamecol.HeaderText<-"First Name"
chrlnamecol.DataPropertyName<-"chrlname"
chrlnamecol.HeaderText<-"Last Name"
//add the datagrid to our form
dataform.Controls.Add(datagrid)
//execute our application
dataform.Show()
Application.Run(dataform)
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:
For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.That's all for now:)

0 comments

Post a Comment

Contributor

My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List

Archives

Got questions? You can reach me here: