Active Reports Dynamic Column Generation for .Net, C#, VB.Net

This article assumes you know a little bit about Data Dynamics Active Reports for .Net, so I am not going to bother going over each step of creating a report from scratch.

This Example is in C# but you can covert it to vb.net very easily.

Download the Complete Source Code for this Article.

This report generates a PDF FILE based on the Active Reports Samples supplied by Data Dynamics.

In order to create Dynamic columns in an Active Report

Create a New Active Report and open it
[Right Click] in the Page Header and in the popup menu select INSERT GROUP HEADER/FOOTER.

Now that your Group Header area has been created we can get started.

Make a note of the names of the Group Header and Detail section.

Click on each area and view the properties.

For my project my Group Header is called "groupHeader1" and my detail area is simply "detail".

Note: The names above are case sensitive in both C# and VB.net.

Note: You can not add controls in the detal format event of the report is actually a row processing event and fires once per-row of data, and at this point the report expects all controls to have already been added to the report.

Now the thing to note about creating Dynamic Columns in Active Reports is that I usually create them
in the Report_Start event and add both a column header / (label) and a data column (textbox).

When generating column labels and text boxes it is up to you to keep track of the X - Y location on the screen of  each controls.

We accomplish this by creating a variable called xPosition and fieldWidth to use to track where on the row we are as shown in the example:

xPosition is the calculated X position on the screen where the label and texbox will be created
fieldWidth is used to add to the xPosition on the screen for adding a new column to the right of the current column

Dynamic Columns in Data Dynamics Active Reports

            float xPosition = 0F;     // Used to save the X position as the fileds and columns are created across the report
            float fieldWidth = 1.0F;  // The default width of the field to be displayed
            xPosition += fieldWidth + 0.2F; // calculate the position of the next field also
                                            // add a little padding to seperate the columns from each other on the report

When creating each label and text box you want to make sure that you add each control to the correct section of the report

Adding labels to the Group Header:

Before we can add a control to the controls collection we need to issue a new to the control.
Otherwise while we are processing in our loop the new control name will be the same.
So here we use the iLoop counter to append to the label name ( we do the same thing with the text box below)
dyHeaderLabel.Name = "dyHeaderLabel" + iLoop;

Now we set the label text to be equal to the name of the column in our dataset table.
dyHeaderLabel.Text = oDataColumn.ColumnName.ToString().Trim().ToUpper();

Here we add the control to the groupHeader1 section of our report.
this.Sections["groupHeader1"].Controls.Add(dyHeaderLabel);

Adding the text boxes to the Detail area:

Assign the datafield of the text box for the data binding of the Detail Format event
dyTextBox.DataField = oDataColumn.ColumnName.ToString();

Give the control a unique name by appending iLoop counter to the name
dyTextBox.Name = "dyTextBox" + iLoop;

Add the control to the details control collection
this.detail.Controls.Add(dyTextBox);

Report_Start

    private void MyReport_ReportStart(object sender, EventArgs e)
    {
        string sError = "";
        try
        {
            float xPosition = 0F;      // Used to save the X position as the fields and columns are created across the report
            float fieldWidth = 1.0F;  // The default width of the field to be displayed

            DataSet oDataSet;
            oDataSet = (DataSet)this.DataSource;

            Int16 iLoop = 0;

            foreach (DataColumn oDataColumn in oDataSet.Tables[0].Columns)
            {

                iLoop++;

                //----------------------------------------------------------------------------------------------
                //- ADD THE LABELS TO THE GROUP HEADER AREA OF THE REPORT
                //----------------------------------------------------------------------------------------------

                Label dyHeaderLabel = new Label();

                dyHeaderLabel.Name = "dyHeaderLabel" + iLoop;
                dyHeaderLabel.Text = oDataColumn.ColumnName.ToString().Trim().ToUpper();
                dyHeaderLabel.Visible = true;
                dyHeaderLabel.Width = fieldWidth;
                dyHeaderLabel.Font = new System.Drawing.Font("Arial", 8, System.Drawing.FontStyle.Bold);  //BOLD THE HEADER LABELS
                dyHeaderLabel.Location = new System.Drawing.PointF(xPosition, 0.0F);

                this.Sections["groupHeader1"].Controls.Add(dyHeaderLabel);

                //----------------------------------------------------------------------------
                //- ADD THE TEXT BOX FIELDS TO THE DETAILS AREA OF THE REPORT
                //----------------------------------------------------------------------------
                TextBox dyTextBox = new TextBox();
                dyTextBox.Name = "dyTextBox" + iLoop;
                dyTextBox.DataField = oDataColumn.ColumnName.ToString();
                dyTextBox.Visible = true;
                dyTextBox.Width = fieldWidth;
                dyTextBox.Font = new System.Drawing.Font("Arial", 8, System.Drawing.FontStyle.Regular);
                dyTextBox.Location = new System.Drawing.PointF(xPosition, 0.0F);

                this.detail.Controls.Add(dyTextBox);

                xPosition += fieldWidth + 0.2F; // add a little padding to seperate the columns from each other on the report

            }

        }
        catch (Exception ex)
        {
            sError = ex.Message;
        }
        finally
        {

        }
    }

posted @ Saturday, July 07, 2007 2:09 PM

Print

Comments on this entry:

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by bbk at 4/1/2008 10:40 AM
Gravatar
how to remove Evaluation Massage under report?!

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by miriam at 4/15/2008 3:09 AM
Gravatar
where can i ask my question about columns in active report?

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by sasikumar at 7/10/2008 8:45 AM
Gravatar
how to use reprot

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by bhavesh at 10/3/2008 5:04 AM
Gravatar
HOME

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by Larry at 10/10/2008 4:44 PM
Gravatar
To remove the Evaluation Massage under report you have to have a LIC key from data dynamics.

In the code you assign the key and the message will no longer display.

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by Larry at 10/10/2008 4:50 PM
Gravatar
Here or at http://www.DataDynamics.com

#  Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by HAMID at 10/17/2008 5:24 AM
Gravatar
vay

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by Adam at 7/13/2009 5:48 PM
Gravatar
Is there a way to avoid the columns being cut off if you are displaying more than the page can show? Automatically moving them to another page perhaps?

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by Raphael at 8/12/2009 4:23 PM
Gravatar
Hi every body. I'll needed a counter there count me how many controls in the detail section is indicated (when it have multipe rows in the detail section). Have any body a idea how i can loosening that problem?
I know the controls has the Property Summery function Count, but it doesn't make that what i will. It always writing a Text as a Value (the Value of the Control). I needed the Value of count as a number. Thank's

# re: Active Reports Dynamic Column Generation for .Net, C#, VB.Net

Left by Ken at 12/3/2009 3:08 PM
Gravatar
Thanks for this article. It helped me with what I was doing. I had a pivot-based datasource which had an unknown number of columns so this was just what I needed.

I adapted it slightly to box them in like a grid and to centre it on the page horizontally as well. I just used this after adding all the controls:

float xOffset = (this.PrintWidth - xPosition) / 2;

then I iterated through and shifted them all along by that amount.

I only wish the Chart Control had an easy way to generate several series from the columns.

Your comment:



 (will not be displayed)


 
 
 
Please add 2 and 3 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910