Microsoft SDK for Java

Data Binding to Tables

Tables also have data binding capabilities. Using a com.ms.wfc.data.ui.DataSource object, you can bind data to your table, as shown in the following sample code.

Example

.
import com.ms.wfc.data.*;
import com.ms.wfc.data.ui.*;
.
void private initForm( ){
   
   DhTable dataTable = new DhTable();
   dataTable.setBorder( 1 );
   dataTable.setAutoHeader( true );
   
   DataSource dataSource = new DataSource();
   dataSource.setConnectionString("DSN=Northwind");
   dataSource.setCommandText("SELECT * FROM Products" );
   
   // If you would like to use the table on the server,
   // call dataSource.getRecordset() to force the DataSource
   // to synchronously create the recordset; otherwise,
   // call dataSource.begin(), and the table will be populated
   // when the recordset is ready, asynchronously.
   if ( !getServerMode() ){
      dataSource.begin();
      dataTable.setDataSource( dataSource );
   } else
       dataTable.setDataSource( dataSource.getRecordset() );

   setNewElements( new DhElement[] { dataTable } );
}

If you know the format of the data that is going to be returned, you can also specify a template (repeater) row that the table will use to format the data that is returned.

To specify a template row

  1. Create your DhTable element.
    DhTable dataTable = new DhTable();
    
  2. Create your template row and set it into the table. You can also (optionally) create a header row. For each item in the template cell that you would like to receive data from the recordset, create a DataBinding for it.
    .
    DhRow repeaterRow = new DhRow();
    RepeaterRow.setBackColor( Color.LIGHTGRAY );
    RepeaterRow.setForeColor( Color.BLACK );
    DataBinding[] bindings = new DataBinding[3];
    DhCell  cell = new DhCell();
    DataBinding[0] = new DataBinding( cell, "text", "ProductID" );
    repeaterRow.add( cell );
    cell = new DhCell();.
    DataBinding[1] = new DataBinding( cell, "text", "ProductName" );
    cell = new DhCell();.
    cell.setForeColor( Color.RED );
    cell.add( new DhText( "$" ) );
    DhText price = new DhText();
    price.setFont( Font.ANSI_FIXED );
    DataBinding[2] = new DataBinding( price, "text", "UnitPrice" );
    cell.add( price );
    repeaterRow.add( cell );
    
    // Set up the table repeater row and bindings.
    table.setRepeaterRow( repeaterRow );
    table.setDataBindings( bindings );
    
    // Create and set the header row.
    DhRow headerRow = new DhRow();
    headerRow.add( new DhCell( "ProductID" ) );
    headerRow.add( new DhCell( "Product Name" ) );
    headerRow.add( new DhCell( "Unit Price" ) );
    table.setHeaderRow( headerRow );
    
  3. Create a DataSource object, and set it to retrieve data in the format you expect.
    DataSource ds = new DataSource();
    ds.setConnectionString("DSN=Northwind");
    ds.setCommandText("SELECT ProductID, ProductName, UnitPrice FROM 
    Products WHERE UnitPrice < 10" );
  1. Set the DataSource into the DhTable object.
    table.setDataSource( ds );
    ds.begin();
    
  2. Add the DhTable to the document.
    setNewElements( new DhElement[] { table } );
    // Alternately, add( table );.

Your table is now populated with the data from the recordset and formatted like the template row.

© 1999 Microsoft Corporation. All rights reserved. Terms of use.