Home >

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

18. August 2010

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.

A Link to the file for download is located at the bottom of this post.

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

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
        {

        }
    }

 

DYARColumns.zip (144.27 kb)

Comments

8/28/2010 7:18:29 PM #
I am tired of these get rich quick schemes.  What I do is, submit my articles to article sites and get new visitors.
9/6/2010 12:06:06 AM #
I see now. I've been wondering about that. Thanks for clearing that up.
9/6/2010 9:29:43 PM #
Very long time reader, 1st time commenter Smile You're writing style is excellent; I am an aspiring journalist myself! In the university currently. Talk to you soon, Mary
9/6/2010 10:37:01 PM #
I must admit, this is not a topic that I am usually interested in reading about, but once I started reading through, I found it to be a really interesting read.
9/7/2010 12:26:41 AM #
I'm diggin' the blog. Lots of interesting info.
9/7/2010 3:52:55 AM #
Wow...Finally someone who knows exactly how to write Tong Do you have a contact page?
9/7/2010 10:48:20 AM #
I really love your blog. Great job!
9/7/2010 1:36:46 PM #
I think You have problem with CSS on FF 3.5. BTW, like your post.
9/7/2010 10:40:07 PM #
If anyone is looking for granite countertops in Fresno, CA I know the best place for quality and great prices..I just got mine installed last week and I love it.
9/7/2010 10:44:04 PM #
You can have a lot of success posting articles to article directories.  You can quickly double your traffic.
9/8/2010 1:39:21 AM #
Submitting articles to article directories is a great way to push traffic to your site. While
9/8/2010 10:27:53 AM #
When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thanks!

Regards!

Phill Mint.


Comments are closed