FlexGrid for WinForms Tutorials > Edit Tutorial > Step 2 of 6: Set Column Types and Formats |
When displaying numeric or date values, you will typically want to adopt a consistent format for the values. The C1FlexGrid control allows you to specify the DataType and Format for each column. These properties can be set either in the designer or in code.
Alternatively, the DataType and Format properties can also be set up through the C1FlexGrid Column Editor:
To specify the DataType and Format for the columns, add the following code after the code added in Step 1 of 6: Create the C1FlexGrid Control for the Edit Tutorial:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' Set the column DataType and Format. Dim c As Column = C1FlexGrid1.Cols("Sales") c.DataType = GetType(Decimal) ' Currency. c.Format = "c2"c = C1FlexGrid1.Cols("Bonus") c.DataType = GetType(Boolean) c.ImageAlign = ImageAlignEnum.CenterCenter |
To write code in C#
C# |
Copy Code
|
---|---|
// Set the column DataType and Format. Column c = c1FlexGrid1.Cols["Sales"]; c.DataType = typeof(Decimal); // Currency. c.Format = "c2"; c = c1FlexGrid1.Cols["Bonus"]; c.DataType = typeof(bool); c.ImageAlign = ImageAlignEnum.CenterCenter; |
The new code formats the Sales column to store and display currency values, and the Bonus column to deal with Boolean values.
If you enter some numeric and non-numeric values in the Sales column, you will notice that the grid won't accept non-numeric entries. The Bonus column displays values as checkboxes, which can be toggled with the mouse or with the keyboard. This is the default behavior for Boolean columns.
Note that the Format property does not affect the data in any way, only how it is displayed.