ComponentOne FlexGrid for WinForms
Cell Content
Using the C1FlexGrid Control > Formatting Cells > Cell Content

To control how the content of the cells is formatted, set the Format property to a format string similar to the ones you use with the String.Format method in the .NET framework. For example, the code below shows short dates on column one and currency values on column two:

To write code in Visual Basic

Visual Basic
Copy Code
' Short date.
_flex.Cols(1).Format = "d"
 
' Currency.
_flex.Cols(2).Format = "c"

To write code in C#

C#
Copy Code
// Short date.
_flex.Cols[1].Format = "d";
 
// Currency.
_flex.Cols[2].Format = "c";

The formatting of cell content can also be set at design time using the Format String dialog box.

The Format String dialog box can be accessed through the Column Tasks menu or through the C1FlexGrid Column Editor.

Note: The Format String dialog box is column specific and will only change the Format property of the selected column.

You can also use custom formats like the ones used in the Visual Basic Format function (for example, "#,###", and so on).

Retrieving Cell Data

You can retrieve the raw grid data using the indexers or the GetData method. To retrieve the formatted data, use the GetDataDisplay method instead. For example:

To write code in Visual Basic

Visual Basic
Copy Code
' Short date.
_flex.Cols(1).Format = "d"
 
' Currency.
_flex.Cols(2).Format = "c"
 
_flex(1, 2) = 10000
Console.WriteLine("Raw value: {0}", _flex(1, 2))
Console.WriteLine("Display value: {0}", _flex.GetDataDisplay(1, 2))
 
' Raw value: 10000
' Display value: $10,000.00

To write code in C#

C#
Copy Code
// Short date.
_flex.Cols[1].Format = "d";
 
// Currency.
_flex.Cols[2].Format = "c";
 
_flex[1, 2] = 10000;
Console.WriteLine("Raw value: {0}", _flex[1, 2]);
Console.WriteLine("Display value: {0}", _flex.GetDataDisplay(1, 2));
 
// Raw value: 10000
// Display value: $10,000.00
See Also