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

Assigning datasource to subreport programatically

5 Answers 359 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
raju
Top achievements
Rank 2
raju asked on 31 Mar 2009, 10:55 AM
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
            Next
        End If

Thanks for your help.
 

5 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 03 Apr 2009, 10:43 AM
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,
Steve
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
raju
Top achievements
Rank 2
answered on 21 Apr 2009, 09:24 AM
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.

Thanks
0
Steve
Telerik team
answered on 21 Apr 2009, 09:55 AM
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,
Steve
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.
0
David Yardy
Top achievements
Rank 1
answered on 16 Mar 2016, 08:05 PM
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'
0
Stef
Telerik team
answered on 21 Mar 2016, 09:53 AM
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.

Regards,
Stef
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
raju
Top achievements
Rank 2
Answers by
Steve
Telerik team
raju
Top achievements
Rank 2
David Yardy
Top achievements
Rank 1
Stef
Telerik team
Share this question
or