Visual Basic Concepts

Binding Data to the Hierarchical FlexGrid

Before you can use its features, you must bind data to the Hierarchical FlexGrid. You can bind data to this control either at design time using the Properties sheet, or programmatically at run time.

Once your Hierarchical FlexGrid is bound to a data source, the design-time display within the Hierarchical FlexGrid is a single blank column and single blank row. The field and band information is not automatically retrieved (to obtain this information, see Retrieving Structure). If the Hierarchical FlexGrid is run without field and band information, the data displays using the default property settings. That is, if the Hierarchical FlexGrid is bound to a hierarchical Command, the bands of data display horizontally with each band containing a column for each field in the Recordset.

Hierarchical FlexGrid bound to the Data Source

Binding Data at Design Time

This section describes how to bind the Hierarchical FlexGrid to a data source at design time.

To bind the Hierarchical FlexGrid at design time

  1. Create a source of data for your Hierarchical FlexGrid.

    The data source can be a DataEnvironment object or an ActiveX® Data Object (ADO) Data Control, a new Visual Basic feature. For this procedure, create the data source as a DataEnvironment object.

  2. On the Visual Basic toolbox, click the MSHFlexGrid control, and then drop it on a Visual Basic .

    -or-

    On the Visual Basic toolbox, double-click the MSHFlexGrid control to drop it on a Visual Basic form.

  3. On the Visual Basic Properties window, set the DataSource property to the DataEnvironment object that contains the Command object that you want to bind to the Hierarchical FlexGrid.

    Caution   If the DataSource is reset, any user-defined modified data in the Hierarchical FlexGrid cells is lost.

  4. On the Visual Basic Properties window, set the DataMember property to a Command object in the Data Environment. If you want to view hierarchical data in your Hierarchical FlexGrid, you must specify a top-most, parent Command object in a Command hierarchy as DataMember.

  5. To view the data in the Hierarchical FlexGrid, on the Run menu, select Start.

    -or-

    Press F5.

Programmatically Binding Data to the Hierarchical FlexGrid

This section describes how to bind data to the Hierarchical FlexGrid programmatically.

To set the DataSource programmatically

  1. On the Visual Basic toolbox, double-click the MSHFlexGrid control to place it on a Visual Basic form.

  2. Right-click your Hierarchical FlexGrid and select View Code from the shortcut menu. The Code Editor window appears.

  3. In the Form_Load event, add code to create an ADO Recordset and assign it to the Hierarchical FlexGrid. This code is provided in the following sub-steps.

    Note   To set the data source programmatically, the project must have a reference to Microsoft ActiveX Data Objects: On the Project menu, choose Select References, and then select Microsoft ActiveX Data Objects 2.0 Library.

    • Create an ADO Connection and Recordset by inserting the following code, replacing the comments as appropriate (for example, replace <myDataSource> with the actual name of your data source):

      DIM Cn As New Connection, Rs As New Recordset
      
      ' You need to replace <myDataSource> with a valid
      ' DSN on your system.
      Cn.ConnectionString = "DSN=<myDataSource>"
      
      ' Use the following code for SHAPE Commands
      Cn.Provider = "MSDataShape"
      Cn.CursorLocation = adUseNone
      
      ' Alternatively, for SQL Commands, use the following code
      Cn.CursorLocation = adUseNone
      
      Cn.Open
      
      ' You need to specify a valid data source for
      ' your Recordset for the Connection created above
      Rs.Source = "<valid SQL SELECT command>"
      
      ' Now associate the Command with the Connection
      ' and execute them.
      Set Rs.ActiveConnection = Cn
      Rs.Open
      
    • Assign the open Recordset in Rs to the Hierarchical FlexGrid by inserting the following code:

      Set MSHFlexGrid1.DataSource = Rs
      
  4. To view the data in the Hierarchical FlexGrid, on the Run menu, select Start.

    -or-

    Press F5.