Data Binding

In Visual Basic, you may select the DataSource property at design time and select from a list of available data sources. This is not done by the controls, but by VB itself. In Visual J++, the DataSource property is not displayed in the Property window. This is because J++ uses a different type of design-time mechanism, and that is why you don't see the DataSource property at design time (not on the VSFlexGrid, MSDataGrid, or any other ActiveX OLEDB control).

To implement data binding in J++, you have two options: use the native WFC DataSource control, or use the ADO classes and create the data source yourself. Either way, you need to connect the grid to the data source using code.

a) To use the native WFC DataSource control, follow these steps:

·      Put a WFC DataSource control on the form.

Set the connectionString and commandText properties to the data you want.

·      Put a VSFlexGrid  control on the form.

Set the Editable, DataMode, and VirtualData properties to the values you want.

·      Bind the VSFlexGrid control to the DataSource control using the following code:

  public class Form1 extends Form {

    public Form1() {

      // Required for Visual J++ Form Designer support

      initForm();

      // Set FlexGrid properties.

      fg.setEditable(True);

      fg.setDataMode(vsflex8.DataModeSettings.flexDMBound);

      // Bind FlexGrid to dataSource control.

      msdatsrc.DataSource ds = (msdatsrc.DataSource)

                               dataSource1.getRecordset().getDataSource();

      fg.setDataSource(ds);

    }

  }

b) To use the VSFlexGrid control with ADO data objects, follow these steps:

·      Import the WFC ADO package with the following import statement:

    import com.ms.wfc.data.*;

·      Declare a Recordset variable and initialize it when the form loads

·      Bind the Recordset to the VSFlexGrid control as before:

  public class Form1 extends Form {

    private Recordset m_rs;

    public Form1() {

      // Required for Visual J++ Form Designer support

      initForm();

      // create ADO recordset

      m_rs = new Recordset();

      m_rs.open("SELECT * FROM CUSTOMERS", "NORTHWIND",

                       AdoEnums.CursorType.STATIC,

                       AdoEnums.LockType.BATCHOPTIMISTIC);

      // Bind FlexGrid to ADO recordset

      msdatsrc.DataSource ds = (msdatsrc.DataSource)m_rs.getDataSource();

      fg.setDataSource(ds);

    }

  }