As you remember, in one of our latest articles we dealt with the problem of fitting the columns width according to the columns content.
At this point we already know that all columns which do not fit the page’s width during report rendering would make rendering engine create a number of pages to the right in order to store the columns that exceed the page’s width. This looks good, but not in case when the table has a couple of parameters and a hundred of columns for year values. Of course we could try switching the position of columns and parameters in the table so the overall view would be quite acceptable, but these changes most probably will not meet the customer’s requirements. One more thing in favor of the upcoming modification is that the table divided into several partitions would be perceived better by the user, unlike viewing the enormous table with tons of data making the whole perception confused.
The main concept
On this step we already have a data source modified for convenient output, as well as calculated columns width values. As far as we see, this part of template responsible for table output should be placed inside a DataBand that would iterate through the pre-modified data source containing the table partitions data. A CrossBand in its turn would be used to output the columns defined in a current partition. In general, all we have to do is to write a script that would create partitions based on the widths of the columns.
A sample to the rescue
To begin with, we should consider the data type that would be used for storing the information about the table partitions. To tell the truth, it would be quite enough to get the index of the first column and the total count of columns in a partition. Let’s describe the structure in CommonScript as follows:
- class TableBlock
- {
- public int StartIndex { get; set; }
- public int Count {get;set;}
- }
A method used for partitioning the table can also be added into the CommonScript. For convenience’ sake, a line containing table names would be duplicated in each partition. Let’s add the partitions list as a data source named ‘linesData’ :
- void SplitDataToLines()
- {
- System.Collections.Generic.List<TableBlock> lines = new System.Collections.Generic.List<TableBlock>();
- DataObjects.Add(“linesData”, lines);
- if(columnWidths.Count == 0)
- {
- return;
- }
- int index = 1;
- double currentBlockWidth = firstColumnWidth + columnWidths[0];
- double pageWidth = page1.Size.Width - page1.Margins.Size.Width;
- TableBlock currentBlock = new TableBlock();
- currentBlock.StartIndex = 0;
- lines.Add(currentBlock);
- while(index < columnWidths.Count)
- {
- if(currentBlockWidth + columnWidths[index] > pageWidth)
- {
- currentBlock.Count = index - currentBlock.StartIndex;
- currentBlockWidth = firstColumnWidth;
- currentBlock = new TableBlock();
- currentBlock.StartIndex = index;
- lines.Add(currentBlock);
- }
- currentBlockWidth += columnWidths[index];
- index++;
- }
- currentBlock.Count = index - currentBlock.StartIndex;
- }
At this point, we defined a data source in CommonScript and would use a CalculateColumnWidth method for our calculations.
- CrossBandSample.TableData data;
With the help of GenerateScript the data source is being retrieved and partitioned:
- try
- {
- // Getting datasource
- data = DataObjects["data"] as CrossBandSample.TableData;
- CalculateColumnWidths();
- SplitDataToLines();
- }
- catch(Exception exc)
- {
- System.Windows.Forms.MessageBox.Show(exc.ToString());
- }
So far so good. Now, we have all the necessary stuff to output the report as we expect it, but some of the template modification is still required. To start with, let’s add an upper level DataBand with ‘linesData’ data source, and then place the rest of the content inside it.
At the end of the DataBand we should place a Detail control that would set the spacing between the table partitions.
Now we are able to use the CrossBand data sources directly, that’s why we can deselect the DataSource property and use InstanceCount. Let’s add the following code to Header’s GenerateScript:
- crossBandColumn.InstanceCount = (int) dataBand2["Count"];
- crossBandValue.InstanceCount = (int) dataBand2["Count"];
At this point we have to fix the code used for setting the columns width, since LineNumber does not match the overall table column numbers any more, and is only valid for columns inside a separate table partition.
In GenerateScript crossBandColumn the following code should be added:
- crossBandColumn.Size = new Vector(columnWidths[(int)dataBand2["StartIndex"] + crossBandColumn.LineNumber], crossBandColumn.Size.Height);
- textBoxColumn.Size = crossBandColumn.Size;
In GenerateScript crossBandValue the code should look as follows:
- crossBandValue.Size = new Vector(columnWidths[(int)dataBand2["StartIndex"] + crossBandValue.LineNumber], crossBandValue.Size.Height);
- textBoxValue.Size = crossBandValue.Size;
After this, we fix the code for getting the column names and the values:
- data.ColumnNames[(int)dataBand2["StartIndex"] + crossBandColumn.LineNumber - 1]
- data.Rows[dataBand1.LineNumber - 1].Values[(int)dataBand2["StartIndex"] - 1 + crossBandValue.LineNumber]
The result is really worth it.
A sample used in this article can be downloaded by the following link http://perpetuumsoft.com/Support/downloads/samples/CrossBandSampleContinueOnPage.zip