ComponentOne FlexGrid for WinForms
Storing and Retrieving Data
Data Binding > Storing and Retrieving Data

The C1FlexGrid control can be used in bound or unbound mode. In bound mode, the grid is connected to a data source, and all the data displayed on the grid comes from the data source. In this mode, changing data on the grid changes it in the underlying data source. In unbound mode, the grid manages its own data source.

In either bound or unbound modes, the easiest way to access data in the C1FlexGrid is using the Row and Column indexers. The indexers allow you to specify a cell in a row or column from which to get or set the data stored there. For example, the following code selects the data in the second cell of a row:

To write code in Visual Basic

Visual Basic
Copy Code
Row[2].Selected = True

To write code in C#

C#
Copy Code
Row[2].Selected = true;

The Item property is another easy way to access data in the C1FlexGrid. The Item property is an indexer that takes row and column indices and gets or sets the data stored in the cell. (You can also use column names as indices.) For example, the following code stores row numbers in the first grid column:

To write code in Visual Basic

Visual Basic
Copy Code
Dim r As Integer
For r = _flex.Rows.Fixed To _flex.Rows.Count - 1
    _flex(r, 0) = r
Next

To write code in C#

C#
Copy Code
for (int r = _flex.Rows.Fixed; r <= _flex.Rows.Count - 1; r++) 
{
    _flex[r, 0] = r;
}

When you assign a value to a cell, the grid tries to convert that value into the column's specified DataType. If the conversion fails, the grid fires the GridError event and does not change the cell. You can override this behavior using the SetData method and setting the coerce parameter to False.

When you retrieve data using the indexers, the grid returns the actual data stored in the cell. To retrieve a string containing the formatted version of the data (what the grid displays to the user), use the GetDataDisplay method.

You can also set and retrieve the contents of the selection using the Clip property. This property is especially useful in handling the clipboard and drag-drop operations. By default, the Clip property returns a string with tab characters (Chr(9)) between cells and return characters (Chr(13)) between rows. To use different delimiters, change the ClipSeparators property.

Finally, you can set and retrieve the contents of arbitrary cell ranges using CellRange objects.

See Also