Articles in this section
Category / Section

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

2 mins read

Add a column with a specific format


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


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

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;
    }
}
AddHandler Me.gridControl1.QueryCellInfo, 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 a column with a specified format in WinForms GridControl.


You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.


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 forums, Direct-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  to leave a comment
Access denied
Access denied