This is a migrated thread and some comments may be shown as answers.

Creating Columns Dynamically Based on Client Configuration

3 Answers 329 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Neela
Top achievements
Rank 1
Neela asked on 15 Nov 2012, 02:58 PM
i have a requirement of generating the columns dynamically for the telerik reporting table, i.e. i have a client configuration if the clients needs a column to be visible i need to display that as part of report, else that column as well as the space occupeid for that column also should not exist.

so i am dynamically generating columns as part of sql query. I will have several tables as part of the result set and i need to bind all the tables to the report except the first table

i have used the code provied by telerik team for dynamically creating the tables for report, in addition to that i have created a panel class to create panel's dynalically

my code is this:
objDS is my data set
for (int i = 1; i <= objDS.Tables.Count-1; i++)
  
{
  
   string strTableName = "Table" + Convert.ToString(i);
  
   string strPanelName = "Panel" + Convert.ToString(i);
  
   // Creating a dynamic panel for adding the Table.
  
   var panel = MyClass.Panel.CreatePanel(strPanelName);
  
   //creating the dynamic table for each table from the result set                
  
   var table = MyClass.Table.CreateTable(strTableName,
  
     unitType: UnitType.Pixel,
  
     locationX: 1,
  
     locationY: 1,
  
     width: 200,
  
     height: 20,
  
     rowHeight: 20,
  
     dataTable: objDS.Tables[i]);
  
   
  
//Creating the columns dynamically
  
for (int k = 0; k <= objDS.Tables[i].Columns.Count - 1; k++)
  
{
  
     table.WithColumn(objDS.Tables[i].Columns[k].ToString(),objDS.Tables[i].Columns[k].ToString(), 120);
  
   
  
}
  
// Creating the header group and body group data dynamically
  
for (int k = 0; k <= objDS.Tables[i].Columns.Count - 1; k++)
  
{
  
                        table.HeaderTextBoxes[objDS.Tables[i].Columns[k].ToString()].Style.BackgroundColor = Color.YellowGreen;
  
                        table.HeaderTextBoxes[objDS.Tables[i].Columns[k].ToString()].Style.BorderStyle.Default = BorderType.Solid;
  
                        table.BodyTextBoxes[objDS.Tables[i].Columns[k].ToString()].Style.BackgroundColor = Color.Yellow;
  
                        table.BodyTextBoxes[objDS.Tables[i].Columns[k].ToString()].Style.BorderStyle.Default = BorderType.Solid;
  
}
  
//adding the dynamically created table to the dynamically created panel.
  
panel.Items.Add(table);
  
//adding the panel along with table to the static panel created from report designer                 
  
this.reportPanel.Items.Add(panel);

The reason why i am creating dynamic panels is i dont know how many tables i will get as part of result set and i dont dont even know how many columns each table contains i.e. 1st table may have only 5 columns and the Nth table may have 10 columns. And i dont even know the position of the Dynamically created table to place in the report.

after table is generated i am adding the table to the dynamically created panel and all the dynamically created panels i am adding them to the panel which is from Reporting front end.

the code is getting executed perfectly can see the columns created and data binded with no exceptions, but in the report the table is showing this error

"An error has occurred while processing Table 'Table1': Missing operator before 'Required' operand"

no clue about this error, need help on this or if there is some other alrernative approach for my requirement please suggest.

Thanks,
NeelaKrishna

3 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 20 Nov 2012, 11:36 AM
Hi NeelaKrishna,

This is a generic error and is most likely caused by invalid expression in your report. Please turn on CLR exceptions and debug your project to get a more accurate error and stack trace. You could also review the Output window in VS and see if it does not contain the stack trace for this error.

If this still does not ring a bell which expression might be causing it, disable them one by one until you locate the invalid one.

Regards,
Steve
the Telerik team

HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!

0
Randhir
Top achievements
Rank 1
answered on 04 Sep 2013, 10:09 AM
I have user Q1 2013 version i have used below code for dynamically column displayed in the table

private void table1_ItemDataBinding(object sender, EventArgs e)
        {
           // get the processing table object since we're in the context of event
            Telerik.Reporting.Processing.Table processingTable = (sender as Telerik.Reporting.Processing.Table);

            FilterValues objFilrs = new FilterValues();

            objFilrs = (FilterValues)HttpContext.Current.Session["filterVal"];
            DataSet ds=new DataSet();
            DataTable dt=new DataTable();
            ds = ImportHistoryBIZ.GetImportHistSearch(objFilrs);
            dt = ds.Tables[1];
            dt = (DataTable)HttpContext.Current.Session["filterVal"];
            processingTable.DataSource = dt;

            //create two HtmlTextBox items (one for header and one for data) which would be added to the items collection of the table
            Telerik.Reporting.HtmlTextBox textboxGroup;
            Telerik.Reporting.HtmlTextBox textBoxTable;

            //we do not clear the Rows collection, since we have a details row group and need to create columns only
            this.table1.ColumnGroups.Clear();
            this.table1.Body.Columns.Clear();
            this.table1.Body.Rows.Clear();
            int i = 0;
            this.table1.ColumnHeadersPrintOnEveryPage = true;
            foreach (DataColumn dc in dt.Columns)
            {
                Telerik.Reporting.TableGroup tableGroupColumn = new Telerik.Reporting.TableGroup();
                this.table1.ColumnGroups.Add(tableGroupColumn);
                this.table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));

                textboxGroup = new Telerik.Reporting.HtmlTextBox();
                textboxGroup.Style.BorderColor.Default = Color.Black;
                textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
                textboxGroup.Value = dc.ColumnName.ToString();
                textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                tableGroupColumn.ReportItem = textboxGroup;

                textBoxTable = new Telerik.Reporting.HtmlTextBox();
                textBoxTable.Style.BorderColor.Default = Color.Black;
                textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
                textBoxTable.Value = "=Fields." + dc.ColumnName;
                textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                this.table1.Body.SetCellContent(0, i++, textBoxTable);
                this.table1.Items.AddRange(new ReportItemBase[] { textBoxTable, textboxGroup });
            }
        }

Please suggest me...



0
Stef
Telerik team
answered on 05 Sep 2013, 04:56 PM
Hi Randhir,

Modifying the report definition once the processing begins is not recommended.

Basically you can create the Table item using the designer or Table Wizard and some sample data, then reuse the generated in the report's designer.cs file code. If the reason for creating the item in events is to allow users to modify data or select only certain columns, you can create custom UI to allow this data manipulation, then create the Table item before passing the report to the ReportViewer. Other approach would be to use filters on the row/column groups to show and hide columns in the Table item.

If you need further help, please open a support ticket and send us a sample project illustrating the scenario.

Regards,
Stef
Telerik

Have you tried the new visualization options in Telerik Reporting Q2 2013? You can get them from your account.

Tags
General Discussions
Asked by
Neela
Top achievements
Rank 1
Answers by
Steve
Telerik team
Randhir
Top achievements
Rank 1
Stef
Telerik team
Share this question
or