)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add the column with specified format in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 20, 2015 |
Last Revised Date: December 20, 2019

Add a column with specific format

To add a column in Grid, you can use the ColCount property to increase the column count and you can use Format property for formatting a column by assigning the CellValueType as int or DateTime

In the following code example, the cell value of the column 4 is set in integer format and column 5 is set in DateTime format.

C#

this.gridControl1.QueryCellInfo += new Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventHandler(gridControl1_QueryCellInfo);
this.bt_add.Click += bt_add_Click;
void bt_add_Click(object sender, EventArgs e)
{
    if (count > this.gridControl1.ColCount)
    {
        //add the column
        this.gridControl1.ColCount += 1;
    }           
}
void gridControl1_QueryCellInfo(object sender, Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs e)
{
    //set the column 4 as integer column
    if (e.ColIndex == 4 && e.RowIndex > 0)
    {
        e.Style.CellValueType = typeof(int);
        e.Style.Format = "#.00";
        e.Style.CellValue = 21.6;
        e.Style.HorizontalAlignment = GridHorizontalAlignment.Right;
    }
    //set the column 4 as Date and time column
    if (e.ColIndex == 5 && e.RowIndex > 0)
    {
        e.Style.CellValueType = typeof(DateTime);
        e.Style.CellValue = new DateTime(2012, 5, 23);
        e.Style.Format = "dd/MM/yyyy";
        e.Style.HorizontalAlignment = GridHorizontalAlignment.Center;
    }
}

VB

Private Me.gridControl1.QueryCellInfo += New Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventHandler(AddressOf gridControl1_QueryCellInfo)
Private Me.bt_add.Click += AddressOf bt_add_Click
Private Sub bt_add_Click(ByVal sender As Object, ByVal e As EventArgs)
  If count > Me.gridControl1.ColCount Then
    'add the column
    Me.gridControl1.ColCount += 1
  End If
End Sub
Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs)
  'set the column 4 as integer column
  If e.ColIndex = 4 AndAlso e.RowIndex > 0 Then
    e.Style.CellValueType = GetType(Integer)
    e.Style.Format = "#.00"
    e.Style.CellValue = 21.6
    e.Style.HorizontalAlignment = GridHorizontalAlignment.Right
  End If
  'set the column 4 as Date and time column
  If e.ColIndex = 5 AndAlso e.RowIndex > 0 Then
    e.Style.CellValueType = GetType(DateTime)
    e.Style.CellValue = New DateTime(2012, 5, 23)
    e.Style.Format = "dd/MM/yyyy"
    e.Style.HorizontalAlignment = GridHorizontalAlignment.Center
  End If
End Sub

After applying the properties, the Grid is displayed as follows.

Columns having integer and datetime format

Figure 1: Columns having Integer and DateTime format

Samples:

C#: FormatC#

VB: FormatVB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon