Skip to main content Skip to footer

How to add dynamic columns at runtime in a FlexReport

In this example, we're reporting side-by-side comparisons of debit and credit for a country at a particular work site (Site1, Site2, etc.) Since we could add new sites at any time, we want to make sure the report can display all the appropriate columns.

1. Define field position and size dynamically.

First, calculate the width of new columns. I subtracted the caption field's width from the full report's width to find the available table width (GrosspageWidth). Since I've already dynamically generated the total number of sites (sitenumber), I can easily find the new width of each of the columns:

//Calculating the available Width to decide column's Width  
double GrosspageWidth = c1FlexReport1.Layout.Width - tbNameCaption.Width - tbNumberCaption.Width - c1FlexReport1.Layout.MarginLeft - c1FlexReport1.Layout.MarginRight;  
double fieldWidth = GrosspageWidth / (sitenumber * 2);  

2. Loop through the values to create new columns.

Now that we have the column width, we can create a loop to generate one column per site:

for (int i = 1; i <= sitenumber; i++)  
{  
//Dynamic Columns  
tbDebit = new Field();  
tbDebit.Name = "Site" + (i).ToString() + "Debit";  
tbDebit.Text = "Site" + i.ToString() + "Debit";  
tbDebit.Left = tbNumberCaption.Width + tbNameCaption.Width + (fieldWidth * (i - 1) * 2);  
tbDebit.Top = 0;  
tbDebit.Width = fieldWidth;  
tbDebit.Height = 0.3f * 1440;  
tbDebit.Calculated = true;  
tbDebit.Align = FieldAlignEnum.CenterMiddle;  
tbDebit.Border = new C1.Win.C1Document.Border(15f, Color.Black, C1.Win.C1Document.DashStyle.Solid);  
tbDebit.Font.Size = 12f;  
sDetail.Fields.Add(tbDebit);  

tbCredit = new Field();  
tbCredit.Name = "Site" + (i).ToString() + "Debit";  
tbCredit.Text = "Site" + (i).ToString() + "Credit";  
tbCredit.Left = tbNumberCaption.Width + tbNameCaption.Width + (fieldWidth * (i - 1) * 2) + fieldWidth;  
tbCredit.Top = 0;  
tbCredit.Width = fieldWidth;  
tbCredit.Height = 0.3f * 1440;  
tbCredit.Calculated = true;  
tbCredit.Align = FieldAlignEnum.CenterMiddle;  
tbCredit.Border = new C1.Win.C1Document.Border(15f, Color.Black, C1.Win.C1Document.DashStyle.Solid);  
tbCredit.Font.Size = 12f;  
sDetail.Fields.Add(tbCredit);  
}  

That's all!