StyledTable

Basic

StyledTable is a component for displaying tabular data in your reports. These data can be static (set during design-time) or dynamic (passed from your program)

Crucial property that determines whether table will show static or dynamic data is DataSource. It is the name of the DataTable object which is expected to be passed from your program to the report. If this property is not set (it's value is empty string), static table data will be displayed. DataSource property takes preceedance over setting static data : you may static data for table, but if DataSource parameter is given, the table will be treated as dynamic.

Parameters

If displaying static data, you can use report parameters to customize the table content. Using parameters is explained in detail in chapter Parameters.

Conditional formatting

You can use AlterDataCondition property to set query for data that need alternative formatting. Rows that fulfill the given criteria will be shown colored in AlterDataColor and AlterDataBackColor. The syntax for setting condition is the same as for setting System.Data.DataColumn.Expression property. Use the designer wizard to create queries in a simple manner.

Totals

In order to calculate column totals, set Stampa.StyledTableColumn.SubtotalType property of the proper column to Sum or Average (which ever fits). Subtotals are calculated only for dynamic tables.

Filtering and Sorting

Dynamic tables are usually filled by linking table instance to report DataSource (by setting table's DataSource property). However, you may reduce the amount of data displayed by setting the FilterExpression of the StyledTable. Filter expresion is infact the WHERE SQL clause for the rows to be displayed. The syntax for setting filter condition is the same as for setting System.Data.DataColumn.Expression property.

You may also set the sorting order of the table data by setting SortExpression. This property contains the ORDER BY clause of the SQL query that will be run over DataSource memory table. Basicly, you should provide a list of columns and sorting order within this property. The syntax for sorting expresion should be like "column1 asc, column2 desc, column3 asc, ...".

Both FilterExpression and SortExpression can contain report parameters (in the form $P{parameter_name}). The parameter values are resolved prior to filter execution. This way, you can filter data in a dynamic fashion, during runtime.

Setting dynamic data

In order to pass data from your program, the DataSource property must be set and correspond to the DataTable.Name property in your code.

// myTable is DataTable previously filled from database
// (using DataAdapter.Fill method, for instance)

DataTable printTable = myTable.Copy();
printTable.Name = "printTable";
// this is the DataSource attribute of my table in XML file

// attach DataTable to printing document
printDocument.AddData(printTable);

// now you're ready for print

Setting static table data in a dynamic fashion

In order to pass data from your program, the Name property must be set for static table.

DataTable staticData = new DataTable("printTable");

// create columns for your table; column names must correspond to your report element StyledTable columns
staticData.Columns.Add("column1", typeof(System.Int32));
staticData.Columns.Add("column2", typeof(System.String));
staticData.Columns.Add("column3", typeof(System.String));

// fill all required data in printTable
for (int i = 0; i<=10;i++)
{
	staticData.Rows.Add(new object[]{i,"Second Column", "Third Column"});
}

// add static data to your table (reportTable)
printDocument.AddStaticData("reportTable", staticTable);

// now you're ready for print

Displaying images in tables

If you want to display bitmaps in your StyledTable, set the Format property of appropriate StyledTableColumn to "Image". Moreover, the corresponding DataColumn of your DataTable should be of type byte[] (simple byte array). If StyledTableColumn.Format is set to "Image", display engine will treat this byte array as image raw data.

In the next example, we'll assume the report having StyledTable with DataSource set to "printTable" and having four columns (id,name,amount,image), where the "image" column has Format property set to "Image". This column is intended to contain barcode images that shall be generated;

Stampa.ReportDocument ReportDocument = new Stampa.ReportDocument();
ReportDocument.setXML("testImages.xml");

// generate some dummy data
DataTable theData = new DataTable("printTable");
theData.Columns.Add("id", typeof(System.Int32));
theData.Columns.Add("name", typeof(System.Int32));
theData.Columns.Add("amount", typeof(System.Int32));
theData.Columns.Add("image", typeof(byte[]));    //     <-- NOTE THIS!

// we need this section object for Barcode constructor
Section section = ReportDocument.Sections[0] as Section;
for (int i = 0; i<=50;i++)
{
	// creating Barcode object - this object is not the part of the report, it's purpose is to get the barcode image
	Stampa.Barcode barcode = new Barcode(0, 0, 100, 30, section); // barcode dimensions are 100x30 pixels
	barcode.Text = Convert.ToString(5000 + i); // this is dummy data - in real case scenario you would put something like article ID
				
	
	// Map, ChartBox, Scatter, Box, Elipse, UserPaint objects also have Image property that you can use for this purpose			
	Bitmap image = barcode.Image;
	MemoryStream s = new MemoryStream();

	// getting image raw data - the format is arbitrary, you can use ImageFormat.Jpeg or ImageFormat.Gif as well,
	// but keep in mind that Jpeg does not handle transparency, and Gif format behaves poorly during compression
	// Png is a good choice, but you can use Jpeg where appropriate, too
	image.Save(s, System.Drawing.Imaging.ImageFormat.Png);
	byte[] rawData = s.ToArray();


	/* Alternative : use custom image from filesystem
	Bitmap image = new Bitmap(@"c:\test\myImage.jpg");
	MemoryStream s = new MemoryStream();
	// we could safely use ImageFormat.Jpeg here, it is obvious that my bitmap is in JPEG format
	image.Save(s, System.Drawing.Imaging.ImageFormat.Png);
	byte[] rawData = s.ToArray();	
	*/
		
	// adding rows to DataTable object		
	theData.Rows.Add(new object[]{i, i*i, i*i, rawData});
}

// add this table to print document
ReportDocument.AddData(theData);

// now you're ready for PDF creation
ReportDocument.SerializeToPdfFile(@"e:\test.pdf");

Preloading data

Table can be filled on report generation by using preload data mechanism. In order to enable this functionality, report has got to have StartInfo.PreloadData property set to True (read more in Report Connection). Once report queries have been executed, report DataSources contain all appropriate database data. In order to associate StyledTable element with DataSource, set DataSource property of the table element to corresponding DataSource name (i.e. query name). The column list of the report table element has got to be a subset of the DataSource columns.

XML structure

Sample XML structure for StyledTable containing static data :

<table x="93" y="773" width="230" height="84"  dataSource="" drawEmptyRows="True" showHeader="False" cellHeight="18" 
  horAlignment="None" verAlignment="None" Layout="EveryPage" Selectable="True">
  <header headerColor="White" headerFontColor="Black">
    <font family="Tahoma" size="8" style="Bold" underline="False" />
  </header>
  <dataRows dataFontColor="Black" backgroundColor="White" alterDataCondition="" alterDataColor="Red" alterDataBackColor="Orange" subtotalsColor="Black">
    <font family="Tahoma" size="8" underline="False" />
    <alternateBackColor value="False" />
    <alternatingBackColor color="Gainsboro" />
  </dataRows>
  <columns>
    <column name="country" label="Country" FormatMask="" subtotalType="None" prefix="" width="150" align="Center" />
    <column name="capital" label="Capital" FormatMask="" subtotalType="None" prefix="" width="80" align="Left" />
  </columns>
  <border color="Black" />
  <data>
    <record>
      <field value="France" />
      <field value="Paris" />
    </record>
  </data>
</table>

Sample XML structure for StyledTable containing dynamic data :

<table x="93" y="773" width="230" height="84"  dataSource="printData" drawEmptyRows="True" showHeader="False" cellHeight="18" 
  horAlignment="None" verAlignment="None" Layout="EveryPage" Selectable="True">
  <header headerColor="White" headerFontColor="Black">
    <font family="Tahoma" size="8" style="Bold" underline="False" />
  </header>
  <dataRows dataFontColor="Black" backgroundColor="White" alterDataCondition="" alterDataColor="Red" alterDataBackColor="Orange" subtotalsColor="Black">
    <font family="Tahoma" size="8" underline="False" />
    <alternateBackColor value="False" />
    <alternatingBackColor color="Gainsboro" />
  </dataRows>
  <columns>
    <column name="country" label="Country" FormatMask="" subtotalType="None" prefix="" width="150" align="Center" />
    <column name="capital" label="Capital" FormatMask="" subtotalType="None" prefix="" width="80" align="Left" />
  </columns>
  <border color="Black" />
</table>

Properties

AlterDataColor Display foreground color for rows that fulfill the AlterDataCondition. Default value : Red
AlterDataBackColor Display background color for rows that fulfill the AlterDataCondition. Default value : AntiqueWhite
AlterDataCondition This property sets the query that will be used to determine which rows will be drawn with alternative color (AlterDataColor). The syntax for setting condition is the same as for setting System.Data.DataColumn.Expression property. Conditions with wrong syntax or wrong column names are not processed (they are treated like String.Empty). Default value : String.Empty
AlternateBackColor Defines if the table will be displayed using two alternating background colors. Default value : False
AlternatingBackColor Second background color for displaying table rows in altrnating colors. Default value : Gainsboro
BackgroundColor Back color of table cells (except header). Default value : White
BorderColor Border color of the cells and table itself. Default value : Black
CellHeight Basic cell height (in pixels). If cell content is multiline, the actual height will be integer multiple of this value . Default value : 18
Columns Collection of defined columns for this table.
DataFont Display font for cell data. Default value : Tahoma, 8pt, Regular
DataFontColor Color for cell data. Default value : Black
DataSource The Name property of the DataTable object passed from user's code. Used for displaying dynamic (database) data. Default value : empty string
DrawEmptyRows Should empty rows be drawn if any space in table area remains unused. Default value : false
DrawHeader Should table header be displayed. Default value : true
FilterExpression Defines the SQL WHERE clause that will be applied on DataSource data before displaying table. Example : "[column1]=10 AND [column2]>1000". You may also use report parameters inside this expression. Default : Empty string.
HeaderBackgroundColor Background color of header cells. Default value : White
HeaderFont Display font for header cell data. Default value : Tahoma, 8pt, Regular
Height Table area rectangle height (in pixels).
HorizontalAlignment StyledTable alignment within page, relative to margins. Default value : None
Name The name of the StyledTable report element.
Selectable This property defines whether object can be selected by left mouse click in design mode. Useful for locking background objects during design mode. If set to false, object can only be selected through Object Browser. Default value : true
SortExpression Defines the sorting order for displayed rows. One sholud provide the list of columns that sorting will be applied to. Example : "column1 asc, column2 desc". Default : Empty string.
StaticData MxN string matrix for static cell data. Used for static tables.
VerticalAlignment StyledTable vertical alignment within page, relative to margins. Default value : None
SubtotalsColor Color for displaying subtotals row. Default value : Color.Black
Width StyledTable width (in pixels).
X Left border coordinate (in pixels).
Y Top border coordinate (in pixels).