Assigning datasource to subreport programatically

6 posts, 0 answers
  1. raju
    raju avatar
    15 posts
    Member since:
    Feb 2009

    Posted 31 Mar 2009 Link to this post

    Hello Telerik team,

    do you have a sample project same as the (master detail report) but using programmatic coding. Just like in the sample,

    1. Created a Department class and an employee class, the department class contain a list of employee as one of the property for the class.
    2. Created as employee report with some details that will serve as my subreport
    3. Created as master class that will hold the department information, then added a subreport control (RptEmployee as the ReportSource)
    4. I assigned the parameters from the UI (DepartmentID)
    5. The datasource was assigned at the WEB UI level (ReportViewer) RptDepartment.datasource

    Now I'm stuck on the Subreport1_needdatasource event (how do I pass the data to the subreport)?
    I do have access to the complete datasource so as the parameter which I can see from InnerReport.Parameter, but how do I go about assigning the right datasource item during need datasource? i got the a sample code below and somehow working but is there a better way?

            Dim sr As Telerik.Reporting.Processing.SubReport = TryCast(sender, Telerik.Reporting.Processing.SubReport)
            If Not sr Is Nothing Then
                Dim deptID As Integer = sr.InnerReport.Parameters.Values(0)
                For Each item As Department In Me.DataSource
                    If item.DepartmentID = deptID Then
                        sr.InnerReport.DataSource = item.Employees
                        Exit For
                    End If
            End If

    Thanks for your help.
  2. Steve
    Steve avatar
    10941 posts

    Posted 03 Apr 2009 Link to this post

    Hi raju,

    The approach you have used is correct and can be found in a few forum threads as well. Can you share what is your concern?

    Kind regards,
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. raju
    raju avatar
    15 posts
    Member since:
    Feb 2009

    Posted 21 Apr 2009 Link to this post

    Hello Steve,

    I was just wondering if there will be a simpler way to assign the datasource to the subreport besides looping through the datasource object and searching for the corresponding DepartmentID.

  4. Steve
    Steve avatar
    10941 posts

    Posted 21 Apr 2009 Link to this post

    Hello raju,

    You have two possibilities to bind data to the subreport.

    The SubReport item placed in the Master report raises NeedDataSource event if the underlining report has DataSource property set to null. As you already explain, SubReport items has not property DataSource. It is a plain report item and its main purpose is to act as a proxy to the underlining report.
    You can write handler similar to this:

                Telerik.Reporting.Processing.SubReport subreportitem = (Telerik.Reporting.Processing.SubReport)sender;
                // subreportitem.DataObject can be used here for retrieving data for the subreport
                subreportitem.InnerReport.DataSource = <data for the subreport>

    NeedDataSource handler is executed for every data row in the Master report.

    Another way to accomplish this is to handle NeedDataSource of the report that is used as subreport. The handler may look similar to this:

                Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
                Telerik.Reporting.Processing.SubReport mastersubreportitem = (Telerik.Reporting.Processing.SubReport )report.Parent          
                // mastersubreportitem.DataObject can be used here for retrieving data for the report
                report.DataSource = <data for the report>

    Please note that we do not discuss how data is retrieved, only where and how it is assigned to the report's DataSource property.

    Sincerely yours,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. David Yardy
    David Yardy avatar
    22 posts
    Member since:
    Aug 2007

    Posted 16 Mar 2016 in reply to Steve Link to this post

    I get this error
    An error has occurred while processing Report '{name of sub report}':
    Unable to cast object of type 'Telerik.Reporting.Processing.SubReport' to type 'Telerik.Reporting.Processing.Report'
  6. Stef
    Stef avatar
    3611 posts

    Posted 21 Mar 2016 Link to this post

    Hi David,

    The error indicates that you are using the processing SubReport element instead of the processing report in an expression or in code.

    Please post the code related to the SubReport item's settings and any code-behind like user functions and events. Also include details about the purpose of any custom code related to the SubReport item.

    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
Back to Top