ComponentOne FlexGrid for WinForms
Differences Between the .NET and ActiveX Versions of C1FlexGrid
FlexGrid for WinForms Overview > Differences Between the .NET and ActiveX Versions of C1FlexGrid

In the ActiveX product, we supplied several versions of the FlexGrid control (ADO, DAO, unbound, Unicode, and so on). In the .NET product, there are two versions: C1FlexGrid and C1FlexGridClassic.

C1FlexGrid is not a simple port of the ActiveX control. It is a brand new grid control, written from the ground up in C#, with the same design principles but with a new object model that is more modern, clean, and powerful than the one in the ActiveX control. The C1FlexGrid control can be bound to ADO.NET data sources or used in unbound mode.

To keep the highest level of source-code compatibility with existing applications, and to make the learning curve as smooth as possible for VSFlexGrid users, we also offer the C1FlexGridClassic control.

C1FlexGridClassic is a control that uses C1FlexGrid as a base class and exposes an object model that is virtually identical to the one in VSFlexGrid. We supply the source code to C1FlexGridClassic so you can see exactly how to use the new object model. You can also use it as an example and create your own grid using the C1FlexGrid as a base class.

If you are writing new applications, you should use the C1FlexGrid control. If you are porting existing applications that use the VSFlexGrid ActiveX control and want to change as little code as possible, then use the C1FlexGridClassic control.

The following table lists the differences between the .NET and ActiveX versions of C1FlexGrid:

VSFlexGrid (ActiveX) C1FlexGrid (.NET)
Rows, Cols Collections The ActiveX control has Rows and Cols properties that are used to get or set the number of rows and columns on the grid. In the C1FlexGrid control, these properties return row and column collections. The collections have read/write properties that return the number of elements and fixed elements in each collection. This is probably the most visible change between the controls. Using the ActiveX control, you would write:
Dim r%, c%
c = 1
For r = _flex.FixedRows To _flex.Rows 
- 1 Debug.Print _flex.TextMatrix(r,c) Next

Using the C1FlexGrid control, this becomes:
Dim r%, c%
c = 1
For r = _flex.Rows.Fixed To 
_flex.Rows.Count - 1 Debug.Print _flex(r,c) Next
Uses the TextMatrix property. Uses indexers.
Styles In the ActiveX control, you can customize the appearance of individual
cells or cell ranges using the Cell property. For example, to give the
second row a red background, you would write:
_flex.Cell(flexcpBackColor, 2, 0, 2, 
_flex.Cols-1)
= vbRed
The C1FlexGrid control uses a CellStyle object to customize cell
appearance. To make the second row red, you would write:
Dim redStyle As CellStyle = _flex.Styles
.Add("Red") redStyle.BackColor = Color.Red _flex.Rows(2).Style = redStyle
But this requires three lines of code instead of one! What's the
advantage? The main advantage of the new approach is that the
new style is an object that can be changed or assigned to new
ranges. For example, if you decide to give the red cells a white
forecolor and a bold font, you can write:
_flex.Styles("Red").ForeColor =
Color.White _flex.Styles("Red").Font =
new Font("Arial", 9, FontStyle.Bold)
This will change the appearance of all cells that use the "Red" style. The previous approach would require either (1) clearing all styles and setting everything up again from scratch or (2) scanning all cells in the grid to detect which cells are red, then changing those. CellStyle objects are used consistently throughout the control, so instead of BackColorFixed and ForeColorSel you can now write Styles.Fixed.BackColor and Styles.Highlight.ForeColor.
CellRange The Cell property is one of the most powerful elements of the VSFlexGrid object model. It allows you to get or set any property of any cell or cell range with a single command. However, handling colors, text, values, and so on. Using a single property means using Variants, and this prevents the compiler from catching many subtle problems in case you make mistakes. The C1FlexGrid replaces the Cell property with a CellRange object that exposes type-safe properties and methods used to access the properties of a cell range. For example, instead of writing:
_flex.Cell(flexcpPicture, 5, 5, 10, 10) 
= theImage

You would write:

       
Dim rg As CellRange
rg = _flex.GetCellRange(5,5,10,10)

rg.Image = theImage

The new approach has two significant advantages: · It is type-safe, so if the variable theImage contained a string instead of an image, you would get a compiler error instead of a runtime error. · You get command-completion when writing the code because the types for each property are known.
Typed columns In the ActiveX version, the ColDataType allowed you to set the type of data that each column contained. This information was used mainly for sorting columns that contained dates or numbers. The .NET version has a Cols[i].DataType property that determines the type of data the column holds. By default, the DataType for all columns is "object", which means you can store anything in any column. You can set the data type to specific types, however, and the grid will try to coerce any data stored in the grid to the proper type. For example:
_flex.Cols[2].DataType = typeof(int);
// Value will be set to 12.
_flex[1, 2] = "12";
// Bad value. Fire the GridError event 
and ignore.
_flex[2, 2] = "hello";
This code would assign the integer 12 to cell (1,2). Cell (2,2) would retain its original value, because the string "hello" cannot be converted to an integer. If you want to store values of mixed types in a column, you have two options:
  1. Set the column's DataType property to "object".
  2. Use the SetData method with the coerce parameter set to False to store a value or object without checking the data type.