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.
. 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
DhTable dataTable = new DhTable();
. 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 );
DataSource ds = new DataSource(); ds.setConnectionString("DSN=Northwind"); ds.setCommandText("SELECT ProductID, ProductName, UnitPrice FROM Products WHERE UnitPrice < 10" );
table.setDataSource( ds ); ds.begin();
setNewElements( new DhElement[] { table } ); // Alternately, add( table );.
Your table is now populated with the data from the recordset and formatted like the template row.