How to wrap columns in Cross Band

In this article we will consider how to display data in the form of a table with fixed number of columns (line-by-line) using the Cross Band element.

To form a table based some data from a Data Source we need to use scripts. Below you can find step-by-step instructions on how to do that. In this sample we will use a DataTable as a DataSource.

1.  Create Data Navigator for your Data Source. To create DataNavigator declare it in the CommonScript of a document:

PerpetuumSoft.Reporting.Data.DataNavigator navigator;

We create Data Navigator to get access to an element required for filling next cell of Cross Band on every step of report creation. 

2.  When generating a document, bind the navigator to the required Data Source (add the following code to the Generate Script of the document):

navigator = PerpetuumSoft.Reporting.Data.DataNavigator.CreateNavigator(Engine.Objects["<DataSourceName>"]);

3.  Add DataBand to a report template and then add the Detail Band to that DataBand. Then add Cross Band to the Detail Band and insert TextBox inside Cross Band. As a result, the document tree of your template should look as follows:
Such elements nesting is required by the following reasons:  
a) DataBand is responsible for forming of table rows.
b) Detail is required for including Cross Band into a report.
c) Cross Band is filled horizontally, left to right and is required for forming elements in one row of a table.
d) In this sample, TextBox is used for displaying values in a table cell.

4.   Using Property Grid, set the Instance Count property of the Cross Band to the N value, where N is the desired number of columns in a table. It is required to limit maximum number of cells by N number in one row of a table.

5.   To form a table it is needed to know number of the rows. We can get it knowing size of the Data Source and number of columns. In the end of the Generate Script of the document add the following line:
 
System.Data.DataTable table = Engine.Objects["<DataSourceName>"] as System.Data.DataTable;
 
Change number of rows in the table using the Instance Count property of the Data Band (if the last row of the table is not filled till the end, then Instance Count needs to be fixed): 

dataBand1.InstanceCount = table.Rows.Count / N + (table.Rows.Count % N > 0 ? 1:0 );

where dataBand1 is the name of the created Data Band on step 3, and N is the desired number of columns in a table.

6.  And finally, add the following code to the Generate Script of the TextBox created on the step 3 to get another value from the table: 

      
if (navigator.HasMoreItems) 
{   
navigator.MoveNext();  
textBox1.Text = (navigator.Current as System.Data.DataRow)["ValueColumn"].ToString();
}
else
{  
textBox1.Visible = false;
}
Here, ValueColumn is column name in a table based on which a table in a report should be filled, and textBox1 is the name of the TextBox created on the 3rd step. 

7.   As a result we get a report with a table filled line by line which includes the required number of columns:


Here you can download the sample: http://perpetuumsoft.com/Support/RSS/CrossBandTable.zip

Add Feedback