Articles in this section
Category / Section

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

2 mins read

Add a column with specific format

To add a column in WinForms GridControl, 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


Conclusion

I hope you enjoyed learning how to add the column with specified format in WinForms GridControl.

You can refer to WinForms Grid control feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Grid control example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied