ComponentOne FlexGrid for WinForms
Step 2 of 6: Set Column Types and Formats
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.

In the Designer

  1. Select the Sales column in the grid. This will open the Column Tasks menu for the Sales column.
  2. Set the Data Type box to Decimal.
  3. Click the ellipsis button in the Format String box to open the Format String dialog box.
  4. Under Format type select Currency.
  5. Click OK to close the Format String dialog box.
  6. Select the Bonus column in the grid. This will open the Column Tasks menu for the Bonus column.
  7. Set the Data Type box to Boolean.

Alternatively, the DataType and Format properties can also be set up through the C1FlexGrid Column Editor:

  1. Open the C1FlexGrid Column Editor by selecting Designer in the C1FlexGrid Tasks menu. For details on how to access the C1FlexGrid Column Editor, see Accessing the C1FlexGrid Column Editor.
  2. Select the Sales column in the right pane.
  3. Set the DataType property to Decimal.
  4. Click the ellipsis button next to the Format property. This will open the Format String dialog box.
  5. Under Format type select Currency.
  6. Click OK to close the Format String dialog box.
  7. Select the Bonus column in the right pane.
  8. Set the DataType property to Boolean.
  9. Click OK to close the editor.

In Code

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;

Run the program and observe the following:

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.

See Also