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

Error create table programmatically "Table Body has X rows but 0 are expected"

1 Answer 176 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 10 May 2017, 12:31 PM

Hi, 

I am trying to create a report that includes a table dynamically generated, I will not use the viewer control, just I would like to generate the table from a DataTable but after compose the with the method "GenerateTable" and save the file I always get the error "An error has occurred while processing Table 'xxxx':
Table Body has 2 rows but 0 are expected"

What is wrong?, any help would be appreciated.

namespace ReportLibrary
{
    partial class GenericReport
    {
        #region Component Designer generated code
        /// <summary>
        /// Required method for telerik Reporting designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Telerik.Reporting.Drawing.StyleRule styleRule1 = new Telerik.Reporting.Drawing.StyleRule();
            this.pageHeaderSection1 = new Telerik.Reporting.PageHeaderSection();
            this.detail = new Telerik.Reporting.DetailSection();
            this.pageFooterSection1 = new Telerik.Reporting.PageFooterSection();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            //
            // pageHeaderSection1
            //
            this.pageHeaderSection1.Height = Telerik.Reporting.Drawing.Unit.Inch(1D);
            this.pageHeaderSection1.Name = "pageHeaderSection1";
            //
            // detail
            //
            this.detail.Height = Telerik.Reporting.Drawing.Unit.Inch(2D);
            this.detail.Name = "detail";
            //
            // pageFooterSection1
            //
            this.pageFooterSection1.Height = Telerik.Reporting.Drawing.Unit.Inch(1D);
            this.pageFooterSection1.Name = "pageFooterSection1";
            //
            // GenericReport
            //
            this.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
            this.pageHeaderSection1,
            this.detail,
            this.pageFooterSection1});
            this.Name = "GenericReport";
            this.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D));
            this.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;
            styleRule1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
            new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.TextItemBase)),
            new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.HtmlTextBox))});
            styleRule1.Style.Padding.Left = Telerik.Reporting.Drawing.Unit.Point(2D);
            styleRule1.Style.Padding.Right = Telerik.Reporting.Drawing.Unit.Point(2D);
            this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] {
            styleRule1});
            this.Width = Telerik.Reporting.Drawing.Unit.Inch(6.125D);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
 
        }
        #endregion
 
        private Telerik.Reporting.PageHeaderSection pageHeaderSection1;
        private Telerik.Reporting.DetailSection detail;
        private Telerik.Reporting.PageFooterSection pageFooterSection1;
    }
}

 

 

namespace ReportLibrary
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.Reporting;
    using Telerik.Reporting.Drawing;
 
    /// <summary>
    /// Summary description for GenericReport
    /// </summary>
    public partial class GenericReport : Telerik.Reporting.Report
    {
        private DataSet dataSet;
 
        public GenericReport(DataSet dataSet)
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();           
 
            //
            // TODO: Add any constructor code after InitializeComponent call
            //    
 
            foreach (DataTable dataTable in dataSet.Tables)
            {
                Telerik.Reporting.Table table = null;
                table  = GenerateTable(dataTable);
                this.detail.Items.Add(table);                  
            }                  
        }
 
        private Telerik.Reporting.Table GenerateTable(DataTable dataTable)
        {
            Telerik.Reporting.Table table = new Table();
            table.Name = dataTable.TableName;
             
            //Create columns
            int counterColumn = 0;
            foreach (System.Data.DataColumn column in dataTable.Columns)
            {
                //Add column
                table.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(1D)));            
                 
                //Create group to include column
                Telerik.Reporting.TableGroup tmpTableGroup = new Telerik.Reporting.TableGroup();
                tmpTableGroup.Name = "tableGroup" + counterColumn;
                 
                //Column textBox
                Telerik.Reporting.TextBox tmpTextBox = new Telerik.Reporting.TextBox();
                tmpTextBox.Name = column.ColumnName;
                tmpTextBox.Value = column.ColumnName;
 
                tmpTableGroup.ReportItem = tmpTextBox;
                table.ColumnGroups.Add(tmpTableGroup);
 
                table.Items.Add(tmpTextBox);
 
                counterColumn++;
            }
 
            //Create rows
            int counterRow = 0;
            foreach (System.Data.DataRow row in dataTable.Rows)
            {               
                table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Unit.Pixel(50)));
 
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    Telerik.Reporting.TextBox tmpTextBox = new Telerik.Reporting.TextBox();
                    tmpTextBox.Name = "dd";
                    tmpTextBox.Value = row.ItemArray[i].ToString();
                    tmpTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D));
 
                    table.Body.SetCellContent(counterRow, i, tmpTextBox);                  
                    table.Items.Add(tmpTextBox);
                }
 
                counterRow++;
            }
             
            table.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.20000000298023224D), Telerik.Reporting.Drawing.Unit.Inch(0.30000001192092896D));
                        
            return table;
        }
                      
    }
}

 

1 Answer, 1 is accepted

Sort by
0
FMorales
Top achievements
Rank 1
answered on 11 May 2017, 07:19 AM

I finally realized that I miss to create groups in the rows and this seems to be the reason for the problem.

This code update generates the table.

 

namespace ReportLibrary
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.Reporting;
    using Telerik.Reporting.Drawing;
 
    /// <summary>
    /// Summary description for GenericReport
    /// </summary>
    public partial class GenericReport : Telerik.Reporting.Report
    {
        private DataSet dataSet;
 
        public GenericReport(DataSet dataSet)
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();           
 
            //
            // TODO: Add any constructor code after InitializeComponent call
            //    
 
            foreach (DataTable dataTable in dataSet.Tables)
            {
                Telerik.Reporting.Table table = null;
                table  = GenerateTable(dataTable);
                this.detail.Items.Add(table);                     
            }                  
        }
 
        //private Telerik.Reporting.Table GenerateTable(DataTable dataTable)
        //{
 
        //    Telerik.Reporting.TableGroup tableGroup1 = new Telerik.Reporting.TableGroup();
        //    Telerik.Reporting.TableGroup tableGroup2 = new Telerik.Reporting.TableGroup();
        //    Telerik.Reporting.TableGroup tableGroup3 = new Telerik.Reporting.TableGroup();
        //    Telerik.Reporting.TableGroup tableGroup4 = new Telerik.Reporting.TableGroup();
 
        //    Table table1 = new Telerik.Reporting.Table();
        //    Telerik.Reporting.TextBox textBox1 = new Telerik.Reporting.TextBox();
        //    Telerik.Reporting.TextBox textBox2 = new Telerik.Reporting.TextBox();
        //    Telerik.Reporting.TextBox textBox3 = new Telerik.Reporting.TextBox();
        //    Telerik.Reporting.TextBox textBox4 = new Telerik.Reporting.TextBox();
        //    Telerik.Reporting.TextBox textBox5 = new Telerik.Reporting.TextBox();
        //    Telerik.Reporting.TextBox textBox6 = new Telerik.Reporting.TextBox();
 
    
        //    //
        //    // table1
        //    //
        //    table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(0.94166672229766846D)));
        //    table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(0.94166672229766846D)));
        //    table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(0.94166672229766846D)));
        //    table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D)));
        //    table1.Body.SetCellContent(0, 0, textBox2);
        //    table1.Body.SetCellContent(0, 1, textBox4);
        //    table1.Body.SetCellContent(0, 2, textBox6);
        //    tableGroup1.Name = "tableGroup0";
        //    tableGroup1.ReportItem = textBox1;
        //    tableGroup2.Name = "tableGroup1";
        //    tableGroup2.ReportItem = textBox3;
        //    tableGroup3.Name = "tableGroup2";
        //    tableGroup3.ReportItem = textBox5;
        //    table1.ColumnGroups.Add(tableGroup1);
        //    table1.ColumnGroups.Add(tableGroup2);
        //    table1.ColumnGroups.Add(tableGroup3);
        //    table1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
        //    textBox2,
        //    textBox4,
        //    textBox6,
        //    textBox1,
        //    textBox3,
        //    textBox5});
        //    table1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.5D), Telerik.Reporting.Drawing.Unit.Inch(0.30000001192092896D));
        //    table1.Name = "table1";
        //    tableGroup4.Groupings.Add(new Telerik.Reporting.Grouping(null));
        //    tableGroup4.Name = "detailTableGroup";
        //    table1.RowGroups.Add(tableGroup4);
        //    table1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2.8250000476837158D), Telerik.Reporting.Drawing.Unit.Inch(0.38333332538604736D));
        //    //
        //    // textBox1
        //    //
        //    textBox1.Name = "textBox1";
        //    textBox1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox1.Value = "H0";
        //    //
        //    // textBox2
        //    //
        //    textBox2.Name = "textBox2";
        //    textBox2.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox2.Value = "F0";
        //    //
        //    // textBox3
        //    //
        //    textBox3.Name = "textBox3";
        //    textBox3.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox3.Value = "H1";
        //    //
        //    // textBox4
        //    //
        //    textBox4.Name = "textBox4";
        //    textBox4.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox4.Value = "F1";
        //    //
        //    // textBox5
        //    //
        //    textBox5.Name = "textBox5";
        //    textBox5.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox5.Value = "H2";
        //    //
        //    // textBox6
        //    //
        //    textBox6.Name = "textBox6";
        //    textBox6.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
        //    textBox6.Value = "F2";
 
        //    return table1;
        //}
 
        private Telerik.Reporting.Table GenerateTable(DataTable dataTable)
        {
            Telerik.Reporting.Table table = new Table();
            table.Name = dataTable.TableName;
 
            //Create columns
            int counterColumn = 0;
            foreach (System.Data.DataColumn column in dataTable.Columns)
            {
                //Add column
                table.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(1D)));
 
                //Create group to include column
                Telerik.Reporting.TableGroup tmpTableGroup = new Telerik.Reporting.TableGroup();
                tmpTableGroup.Name = "tableGroup" + counterColumn;
 
                //Column textBox
                Telerik.Reporting.TextBox tmpTextBox = new Telerik.Reporting.TextBox();
                tmpTextBox.Name = "textBox" + counterColumn;
                tmpTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(0.94166666269302368D), Telerik.Reporting.Drawing.Unit.Inch(0.19166666269302368D));
                tmpTextBox.Value = column.ColumnName;
 
                tmpTableGroup.ReportItem = tmpTextBox;
                table.ColumnGroups.Add(tmpTableGroup);
 
                table.Items.Add(tmpTextBox);
 
                counterColumn++;
            }
 
            //Create rows
 
            Telerik.Reporting.TableGroup tmpTableGroupRowForChild = new TableGroup();
            tmpTableGroupRowForChild.Name = "detailTableGroup";
 
            int counterRow = 0;
            foreach (System.Data.DataRow row in dataTable.Rows)
            {
                table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Unit.Pixel(50)));
 
                Telerik.Reporting.TableGroup tmpTableGroup = new TableGroup();
                tmpTableGroup.Name = "group" + counterRow;
 
                tmpTableGroupRowForChild.ChildGroups.Add(tmpTableGroup);
 
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    Telerik.Reporting.TextBox tmpTextBox = new Telerik.Reporting.TextBox();
                    tmpTextBox.Name = "dd";
                    tmpTextBox.Value = row.ItemArray[i].ToString();
                    tmpTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(1D), Telerik.Reporting.Drawing.Unit.Inch(1D));
 
                    table.Body.SetCellContent(counterRow, i, tmpTextBox);
                    table.Items.Add(tmpTextBox);
                }
 
                counterRow++;
            }
 
            tmpTableGroupRowForChild.Groupings.Add(new Telerik.Reporting.Grouping(null));
            table.RowGroups.Add(tmpTableGroupRowForChild);
 
            //table.RowGroups.Add(tmpTableGroupRowForChild);
            table.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2.8250000476837158D), Telerik.Reporting.Drawing.Unit.Inch(0.98333334922790527D));
            table.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.20000000298023224D), Telerik.Reporting.Drawing.Unit.Inch(0.30000001192092896D));
 
            return table;
        }
 
    }
}

 

Tags
General Discussions
Asked by
FMorales
Top achievements
Rank 1
Answers by
FMorales
Top achievements
Rank 1
Share this question
or