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

c# Dynamic Report Class file with HTML5 Report viewer.

2 Answers 724 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 09 Jan 2018, 05:26 PM

Hi,

I have two projects; a website and a web api. (.net Core 2.0 )

The website has the following html;

 $("#reportViewer1")
                .telerik_ReportViewer({
                    //serviceUrl: "/api/reports/",
                    serviceUrl: "@ViewBag.TelerikReportServerUrl",
                    reportSource: {
                        report: "TelerikReportService.Report, TelerikReportService", 
                        //report: "Barcodes Report.trdp",
                        parameters: {}
                    },
                    viewMode: telerikReportViewer.ViewModes.INTERACTIVE,
                    scaleMode: telerikReportViewer.ScaleModes.SPECIFIC,
                    scale: 1.0,
                });

And within the web API project I have been trying to create an example Report Class file that can be served up to the client.
This is how far I have got, but it wont work and I cannot find an example of this anywhere in the forums.

("Barcodes Report.trdp" works fine, just a CODED report class,  I cannot get to work)

I get the error;

"Unable to get report parameters.
An error has occurred.
Invalid report type"

 

CODED report class: 

namespace TelerikReportService

{
    public partial class SchemeReport : Telerik.Reporting.Report
    {

        public SchemeReport()
        {
            
        }


        public DataTable GetData()
    {
        dynamic table = new DataTable();
        table.Columns.Add("col1", typeof(int));
        table.Columns.Add("col2", typeof(int));

        table.Rows.Add(1, 3);
        table.Rows.Add(4, 6);
        table.Rows.Add(2, 5);
        table.Rows.Add(4, 7);

        return table;
    }



    public Telerik.Reporting.Report GenerateReport()
    {
            
        //create a blank report
        Telerik.Reporting.Report report = new Telerik.Reporting.Report();
        report.Name = "Report1";
        report.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0));
        report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;
        report.Width = Telerik.Reporting.Drawing.Unit.Inch(6.0);

        //create a detail section and add it to the report instance's Items collection
        Telerik.Reporting.DetailSection detailSection = new Telerik.Reporting.DetailSection();
        detailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(1);

        detailSection.Name = "DetailSection";
        report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { detailSection });

        //create a blank Table item
        Telerik.Reporting.Table table1 = new Telerik.Reporting.Table();
        table1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.0), Telerik.Reporting.Drawing.Unit.Inch(0.0));
        table1.Name = "Table1";
        table1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(4), Telerik.Reporting.Drawing.Unit.Inch(1));


        //get the data for the table
        dynamic data = GetData();
        table1.DataSource = data;

        //create a dynamic row group
        Telerik.Reporting.TableGroup DetailRowGroup = new Telerik.Reporting.TableGroup();
        DetailRowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
        DetailRowGroup.Name = "DetailRowGroup";
        table1.RowGroups.Add(DetailRowGroup);
        //add a row container
        table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));


        //add columns
        for (int i = 0; i <= data.Columns.Count - 1; i++)
        {
            //add a column container
            table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(2)));
            //add a static column group per data field
            Telerik.Reporting.TableGroup columnGroup = new Telerik.Reporting.TableGroup();
            table1.ColumnGroups.Add(columnGroup);

            //header textbox
            Telerik.Reporting.TextBox headerTextBox = new Telerik.Reporting.TextBox();
            headerTextBox.Name = "headerTextBox" + i.ToString();
            headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
            headerTextBox.Value = data.Columns[i].ColumnName;
            columnGroup.ReportItem = headerTextBox;

            //field that will be displayed
            Telerik.Reporting.TextBox detailRowTextBox = new Telerik.Reporting.TextBox();
            detailRowTextBox.Name = "detailRowTextBox" + i.ToString();
            detailRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
            detailRowTextBox.Value = "= Fields.[" + data.Columns[i].ColumnName + "]";
            table1.Body.SetCellContent(0, i, detailRowTextBox);

            //add the nested items in the Table.Items collection
            table1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
            headerTextBox,
            detailRowTextBox
        });

        }

        //add total group - static group out of the detail row group
        table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));
        Telerik.Reporting.TableGroup totalRowGroup = new Telerik.Reporting.TableGroup();
        totalRowGroup.Name = "totalRowGroup";
        table1.RowGroups.Add(totalRowGroup);
        for (int i = 0; i <= data.Columns.Count - 1; i++)
        {
            Telerik.Reporting.TextBox totalRowTextBox = new Telerik.Reporting.TextBox();
            totalRowTextBox.Name = "detailRowTextBox" + i.ToString();
            totalRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
            totalRowTextBox.Value = "= Sum(Fields.[" + data.Columns[i].ColumnName + "])";

            totalRowTextBox.Style.BackgroundColor = Color.Azure;
            table1.Body.SetCellContent(1, i, totalRowTextBox);
        }


        //add the table in the detail section's Items collection
        detailSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { table1 });


        return report;
    }
    }
}

 

2 Answers, 1 is accepted

Sort by
0
Jason
Top achievements
Rank 1
answered on 10 Jan 2018, 03:12 PM

Latest attempt (Create a blank report in report designer) -With this code I get a blank report also.

namespace TelerikRestService.Reports
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    
    using Telerik.Reporting;
    using Telerik.Reporting.Drawing;

    /// <summary>
    /// Summary description for TestReport.
    /// </summary>
    public partial class TestReport : Telerik.Reporting.Report
    {
        public TestReport()
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            //GenerateReport();
            this.detail.ItemDataBinding += new System.EventHandler(this.detail_ItemDataBinding);
        }

        private void detail_ItemDataBinding(object sender, EventArgs e)
        {
            //this.detail.ColumnGroups.Clear();
            //this.detail.Body.Columns.Clear();
            //this.detail.Body.Rows.Clear();
            GenerateReportx();
            
        }


        public DataTable GetData()
        {
            dynamic table = new DataTable();
            table.Columns.Add("col1", typeof(int));
            table.Columns.Add("col2", typeof(int));

            table.Rows.Add(1, 3);
            table.Rows.Add(4, 6);
            table.Rows.Add(2, 5);
            table.Rows.Add(4, 7);

            return table;
        }



        public void GenerateReportx()
        {

            //create a blank report
            //Telerik.Reporting.Report report = this.Report; // new Telerik.Reporting.Report();
            this.Report.Name = "Report1";
            this.Report.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0), Telerik.Reporting.Drawing.Unit.Inch(1.0));
            this.Report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Letter;
            this.Report.Width = Telerik.Reporting.Drawing.Unit.Inch(6.0);

            //create a detail section and add it to the report instance's Items collection
            Telerik.Reporting.DetailSection detailSection = new Telerik.Reporting.DetailSection();
            detailSection.Height = Telerik.Reporting.Drawing.Unit.Inch(1);

            detailSection.Name = "DetailSection";
            this.Report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { detailSection });

            //create a blank Table item
            
            Telerik.Reporting.Table table1 = new Telerik.Reporting.Table();
            table1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Inch(0.0), Telerik.Reporting.Drawing.Unit.Inch(0.0));
            table1.Name = "Table1";
            table1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(4), Telerik.Reporting.Drawing.Unit.Inch(1));


            //get the data for the table
            dynamic data = GetData();
            table1.DataSource = data;

            //create a dynamic row group
            Telerik.Reporting.TableGroup DetailRowGroup = new Telerik.Reporting.TableGroup();
            DetailRowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
            DetailRowGroup.Name = "DetailRowGroup";
            table1.RowGroups.Add(DetailRowGroup);
            //add a row container
            table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));


            //add columns
            for (int i = 0; i <= data.Columns.Count - 1; i++)
            {
                //add a column container
                table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Inch(2)));
                //add a static column group per data field
                Telerik.Reporting.TableGroup columnGroup = new Telerik.Reporting.TableGroup();
                table1.ColumnGroups.Add(columnGroup);

                //header textbox
                Telerik.Reporting.TextBox headerTextBox = new Telerik.Reporting.TextBox();
                headerTextBox.Name = "headerTextBox" + i.ToString();
                headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
                headerTextBox.Value = data.Columns[i].ColumnName;
                columnGroup.ReportItem = headerTextBox;

                //field that will be displayed
                Telerik.Reporting.TextBox detailRowTextBox = new Telerik.Reporting.TextBox();
                detailRowTextBox.Name = "detailRowTextBox" + i.ToString();
                detailRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
                detailRowTextBox.Value = "= Fields.[" + data.Columns[i].ColumnName + "]";
                table1.Body.SetCellContent(0, i, detailRowTextBox);

                //add the nested items in the Table.Items collection
                table1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
            headerTextBox,
            detailRowTextBox
        });

            }

            //add total group - static group out of the detail row group
            table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.5)));
            Telerik.Reporting.TableGroup totalRowGroup = new Telerik.Reporting.TableGroup();
            totalRowGroup.Name = "totalRowGroup";
            table1.RowGroups.Add(totalRowGroup);
            for (int i = 0; i <= data.Columns.Count - 1; i++)
            {
                Telerik.Reporting.TextBox totalRowTextBox = new Telerik.Reporting.TextBox();
                totalRowTextBox.Name = "detailRowTextBox" + i.ToString();
                totalRowTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Inch(2), Telerik.Reporting.Drawing.Unit.Inch(0.5));
                totalRowTextBox.Value = "= Sum(Fields.[" + data.Columns[i].ColumnName + "])";

                totalRowTextBox.Style.BackgroundColor = Color.Azure;
                table1.Body.SetCellContent(1, i, totalRowTextBox);
            }


            //add the table in the detail section's Items collection
            detail.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { table1 });
            //detailSection.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { table1 });


            //return report;
        }

    }
}

 

 

 

0
Todor
Telerik team
answered on 12 Jan 2018, 12:53 PM
Hi Jason,

I have modified your code (from your last message) to make the report runnable. I have added a WinForms project with WinForms ReportViewer to the solution to display the report.

Note that generally we don't encourage our users to create Telerik Reporting reports programmatically.

We do not recommend also the report event handlers (as ItemDataBinding) to be used for modifying report definition.

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Jason
Top achievements
Rank 1
Answers by
Jason
Top achievements
Rank 1
Todor
Telerik team
Share this question
or