How to Change the HeaderText of DataGridView in F#

This article explains the DataGridviewTextBoxColumn class and how to use it in a Windows Forms application, including how to change the HeaderText.


This article explains the DataGridviewTextBoxColumn class and how to use it in a Windows Forms application and how to change the HeaderText of the DataGridView. I have used the DataGridView Control to bind the datasource database table values.

DataGridviewTextBoxColumn Class

The DataGridviewTextBoxColum class allows us to display and edit the text of a column. The DataGridViewTextBoxColumn class derives from the abstract class DataGridColumnStyle. The DataGridviewTextBoxColumn is automatically associated with the various data types like decimal, double, Byte, string, Int16, Int32, Int64, UInt16, UInt32 and Uint64. The sort mode for this column type defaults to automatic. The HeaderText property of the DataGridviewTextBoxColumn class allows the user to display the caption that is different from the mapping name value, when the mapping value is not understandable then you can use the HeaderText property to reset the name, for example you can change the "EID" to "EmployeeId".

Namespace : System.Windows.Forms


let varname=new DataGridViewTextBoxColumn() 


DataPropertyName Property


When the AutoGenerateColumns property of the DataGridView class is true then each column is automatically set with a DataPropertyName property to the name of a property or you can say database column in the datasource specified by the DataSource property.


Properties of DataGridviewTextBoxColumn class

The following are some of the properties of the DataGridviewTextBoxColumn class:

  • AutosizeMode: This property allows user to set the width size automatically.

  • HeaderText: Using the HeaderText property you can set the caption on the header.

  • DataPropertyName: Change the data source database column-name that is bound by the DataTextBoxColumn.

  • Visible: It determines whether the column is visible.

  • Width: Get or sets the width of the column.

  • SortMode: Using this property you can get or set the sort mode for the column and the default sort mode of the column is automatic.


Create Database & Tables


Create Database Details

use Details

create table EmployeeInfo(

EmpId int primary key identity(100,1),

EmpName varchar(max),

EmpLastName varchar(max),

EmpAddress nvarchar(max),

EmpDepartment varchar(max),

EmpCity varchar(max)



To execute the schema of the table use the following query and press F5:

SELECT * FROM EmployeeInfo



Insert the values into the fields columns using the following:

insert into EmployeeInfo(EmpName,EmpLastName,EmpAddress,EmpDepartment,EmpCity)values('Pankaj','Lohani','a-43 streeno-6 east vinod nagar','Software-Engineer','New Delhi')

insert into EmployeeInfo(EmpName,EmpLastName,EmpAddress,EmpDepartment,EmpCity)values('Nimit','Joshi','a-45 streeno-10 west vinod nagar','Web-Devloper','New Delhi')

insert into EmployeeInfo(EmpName,EmpLastName,EmpAddress,EmpDepartment,EmpCity)values('Pravesh','Khanduri','a-50 streeno-11 vinod nagar','Software-Tester','New Delhi')


The following is the query to execute:

SELECT * FROM EmployeeInfo


Now let's use the following procedure.

Step 1:

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


Step 2:

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



Step 3:

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


Step 4:

Provide the following code for the F# application:

open System 

open System.Windows.Forms 

open System.Data

open System.Data.SqlClient

open System.Drawing  

let constring = @"Data Source=ServerName;Initial Catalog=Details;User Id=; Password="

let con = new SqlConnection(constring)

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

let ds = new DataSet() 


let dataform = new Form(Text="Change DataGrid HeaderText",ClientSize=new System.Drawing.Size(450, 232))   

let empdatagrid = new DataGridView(Size=new System.Drawing.Size(600, 143)) 

let empidcol=new DataGridViewTextBoxColumn() 

let empfnamecol=new DataGridViewTextBoxColumn() 

let emplnamecol=new DataGridViewTextBoxColumn()   

let empaddcol=new DataGridViewTextBoxColumn()  

let empdeptcol=new DataGridViewTextBoxColumn() 

let empcitycol=new DataGridViewTextBoxColumn() 








empdatagrid.DataSource <- ds.Tables.["empInfo"]                                                                                                                                                                                      




empfnamecol.HeaderText<-"First Name" 


emplnamecol.HeaderText<-"Last Name" 






empcitycol.HeaderText<-"City Name" 





Step 5:

Debug the application by pressing F5 and the result of the application will be as in the following figure:


See the datasource column fields name that has been changed in the DataGridView headertext.



In this article you saw how to change the HeaderText of DataGridView in a Windows Forms application. First we explained the DataGridviewTextBoxColumn class then we explained DataPropertyName.