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
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