Set CrossTab DataSource in code-behind

5 posts, 0 answers
  1. Marcello
    Marcello avatar
    78 posts
    Member since:
    Apr 2013

    Posted 04 Nov 2014 Link to this post

    Hi,

    I develop my reports with a standard sql connection.
    Contrariwise, when I consume the reports in my application I set datasource programmatically (report.DataSource=<DataTable>).
    All work good if the report is a simple report.
    If it contains a CrossTab the ReportViewer fails and show a message as "An error has occurred while processing Table 'crosstab1': Unable to establesh a connection to the database..."

    Thanks,
    marc.
  2. Marcello
    Marcello avatar
    78 posts
    Member since:
    Apr 2013

    Posted 06 Nov 2014 in reply to Marcello Link to this post

    No suggestions...?
    marc.
  3. Stef
    Admin
    Stef avatar
    3610 posts

    Posted 06 Nov 2014 Link to this post

    Hi Marc,

    The Crosstab item is a data item, having its own DataSource property. to set its DataSource at run-time you can access the item from the report's Items collection as follows:
    var report=new MyReport();
    var crosstab= report.Items.Find("crosstab1",true)[0] as Telerik.Reporting.Crosstab;
     
    report.DataSource=GetReportData();
    crosstab.DataSource=GetCrosstabData();
     
    reportViewer1.ReportSource = new InstanceReportSource { ReportDocument = report};

    I hope the above information is helpful.

    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  4. Marcello
    Marcello avatar
    78 posts
    Member since:
    Apr 2013

    Posted 06 Nov 2014 in reply to Stef Link to this post

    Hi Stef,

    thanks very much.
    In the application that consumes the report, I don't know objects contained in the report.
    How to get all Items that require a DataSource?

    Thank.
  5. Marcello
    Marcello avatar
    78 posts
    Member since:
    Apr 2013

    Posted 06 Nov 2014 in reply to Marcello Link to this post

    Hi,

    this works good for me:

    private static void SetItemsDataSource(Telerik.Reporting.ReportItemBase.ItemCollection items, System.Data.DataTable source)
            {
                foreach(Telerik.Reporting.ReportItemBase i in items)
                {
                    if (i is Telerik.Reporting.DataItem)
                        ((Telerik.Reporting.DataItem)i).DataSource = source;
     
                    SetItemsDataSource(i.Items, source);
                }
            }

    And I call it as:

    SetItemsDataSource(report.Items, t);

    marc.
Back to Top