ComponentOne VSFlexGrid 8.0
FlexDataSource Property

Returns or sets a custom data source for the control.

Syntax

[form!]VSFlexGrid.FlexDataSource[ = IVSFlexDataSource ]

Remarks

The VSFlexGrid control can be bound to several types of data source, including ADO or DAO recordsets, Variant arrays, and other VSFlexGrid controls. The FlexDataSource property is yet another option, based on a custom COM interface that is easy to implement and very flexible.

The main advantages of data-binding through the FlexDataSource property are speed and flexibility. The main disadvantage is that you have to write more code than with the other options. You should consider using the FlexDataSource property when you have large amounts of data stored in custom structures or objects (other than database recordsets). By using the FlexDataSource property, you may display and edit the data in-place. There is no need to copy it to the grid and save it back later. In fact, the data may even be mostly virtual, consisting of dynamically calculated values rather than static information.

When you assign a new data source to the FlexDataSource property, the control will automatically set each column's header text and ColKey property based on the data source's field name.  You may change these values after setting the FlexDataSource property, if you wish.

Note

When bound to a FlexDataSource, values on fixed rows are not regarded as bound data.  You may set or retrieve them with code without affecting the data source.  Fixed columns, on the other hand, are regarded as bound data.  Their contents are read from and written to the data source.

To bind the VSFlexGrid control to a FlexDataSource property, you need to implement an object that exposes the IVSFlexDataSource COM interface. The interface is very simple. It consists of only two methods: GetData and SetData. GetData(Row, Col) returns a string to be displayed by the grid at the specified position. It is called by the grid when it needs to display a value. SetData(Row, Col, Data) updates the data source at the specified position with the new value. It is called by the grid when the user edits a cell.

The distribution CD includes sample code that show how to implement and use the FlexDataSource object in a fairly realistic scenario, both in Visual Basic and Visual C++. The following tutorial is a very simple example of how to use the FlexDataSource property.

Step 1: Create the project

Start a new Visual Basic project and add a VSFlexGrid control to the form (you may use any version: ADO, DAO, or Light). Chang the name of the grid control to fs.

Step 2: Create the FlexDataSource object

Add a new class module to your project by selecting the "Project|Add Class Module" command from the Visual Basic menu. On the new class code window, type the following statement:

    Implements IVSFlexDataSource

This tells the world that the new class implements the IVSFlexDataSource methods and that they are available to anyone who cares to use them. Now select the IVSFlexDataSource item from the "Object Box" (the drop-down list on the top left of the code window). VB will immediately create a "stub" (empty function) for the GetFieldCount method. Now click on the "Procedures/Events Box" (the drop-down list on the top right of the code window) and select each method to create stubs for each one. Here is what the code window should look like by now:

    Option Explicit

    Implements IVSFlexDataSource

   

Private Function IVSFlexDataSource_GetFieldCount() As Long

End Function

   

Private Function IVSFlexDataSource_GetFieldName(ByVal Field As Long) As String

End Function

   

Private Function IVSFlexDataSource_GetRecordCount() As Long

End Function

   

Private Function IVSFlexDataSource_GetData(ByVal Field As Long, ByVal Record As Long) As String

End Function

   

Private Sub IVSFlexDataSource_SetData(ByVal Field As Long, ByVal Record As Long, ByVal newData As String)

End Sub

Step 3: Implement the Data Structure

In this example, the data will be completely virtual. We will simply display a table of angles in degrees and radians, their sines, and co-sines. The table will have four "fields" and 360 "records". Here is the code needed to implement this structure:

Private Function IVSFlexDataSource_GetFieldCount() As Long

        IVSFlexDataSource_GetFieldCount = 4

End Function

   

Private Function IVSFlexDataSource_GetRecordCount() As Long

        IVSFlexDataSource_GetRecordCount = 360

End Function

   

Private Function IVSFlexDataSource_GetFieldName(ByVal Field As Long) As String

        Select Case Field

            Case 0: IVSFlexDataSource_GetFieldName = "Angle (Deg)"

            Case 1: IVSFlexDataSource_GetFieldName = "Angle (Rad)"

            Case 2: IVSFlexDataSource_GetFieldName = "Sine"

            Case 3: IVSFlexDataSource_GetFieldName = "Co-Sine"

        End Select

    End Function

Now that we have defined the data structure, we need to implement the functions that will supply the actual data.

Step 3: Implement the GetData and SetData methods

The GetData method is responsible for providing data to the consumer. In this example, there is no static data, only calculated fields:

Private Function IVSFlexDataSource_GetData(ByVal Field As Long, ByVal Record As Long) As String

        Select Case Field

            Case 0: IVSFlexDataSource_GetData = Record

            Case 1: IVSFlexDataSource_GetData = Record / 180# * 3.1416

            Case 2: IVSFlexDataSource_GetData = Sin(Record / 180# * 3.1416)

            Case 3: IVSFlexDataSource_GetData = Cos(Record / 180# * 3.1416)

        End Select

    End Function

The SetData method is responsible for updating the data new information supplied by the user (e.g. by editing a grid cell). In this case, the data cannot be changed, so any attempt to do it will simply raise an error:

Private Sub IVSFlexDataSource_SetData(ByVal Field As Long, ByVal Record As Long, ByVal newData As String)

        Err.Raise 666, "IVSFlexDataSource", "This data is read-only."

End Sub

The SetData method is trivial in this case, but in a more realistic application it could be used to perform data-validation and to allow editing of certain columns only.

Step 5: Hook up the VSFlexGrid and the Data Provider

Now that the FlexDataSource object is ready, all we need to do is hook it up to the grid. Double click on the main form (the one with the grid on it), and add the following code to the Form_Load event:

    Private Sub Form_Load()

        Dim fds As New Class1     ' create the data source object

        fg.FlexDataSource = fds   ' assign it to the grid

        fg.ColFormat(-1) = "#.##" ' format grid columns

        fg.ColFormat(0) = ""

    End Sub

Run the project and you will see that it loads very quickly, and displays the information as expected. Just for fun, try changing the value returned by the GetRecordCount method to a really large value (say 500,000 or so) and run the project again. You will notice there's little or no speed degradation.

This is the end of the tutorial. For more details, refer to the samples on the distribution CD.

Data Type

IVSFlexDataSource

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback