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

SubReport DataBinding

7 Answers 284 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Yonggu Kang
Top achievements
Rank 1
Iron
Yonggu Kang asked on 07 Aug 2013, 03:39 PM
Hi, telerik,

I have 1 parent business object which has 4 child list objects which should be printed in subreport.
So I created 1 report with 4 seperate subreport, but spend quite lot of time how to bind witrh business object.
I've read your document and read multiple threads here and tried as followings showed in another thread
but still have no success. When hitting static method to get reportsource,souce is always null.
 
// i'm using needdatasource event to fetch data with report parms
void RptSaleDailyView_NeedDataSource(object sender, EventArgs e)
     {
         Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
         .
         .
        // fetch data into business objects.
         var list = MyBizObject.GetReportData();
 
         // i'm sure my bisiness object has records for various child objects.
         this.objectDataSource1.DataSource = list;
 
         report.DataSource = this.objectDataSource1;
 
         //// sub report binding
         this.subMainPart.Bindings.Add(new Telerik.Reporting.Binding("ReportSource", "= GetSaleMainPartReportSource(ReportItem.DataObject.DayReportList)"));
    }
 
     public static ReportSource GetSaleMainPartReportSource(object source)
     {
         var report = new RptDailyMainPartView();
         report.DataSource = source;      
        return new InstanceReportSource { ReportDocument = report };
     }

So, I read another thread to set subreport's constructor as follows;

    public RptDailyMainPartView()
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            this.Bindings.Add(new Telerik.Reporting.Binding("DataSource", "=ReportItem.Parent.DataObject.DayReportList"));


But this touches parent so early before fetching data.
I completly lost a way how to do it. Pls advise me.

Thank you in advance.

Kang







7 Answers, 1 is accepted

Sort by
0
Accepted
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 07 Aug 2013, 08:49 PM
Perhaps my code might be of assistance.  This uses a data table however it could easily be changed to use a list of objects.  The property "subReportName" is the name of the field (in the report) which holds the subreport, not the name of the subreport itself.


#region Class Method - SetSubReport
/// <summary>
/// A method which sets the subreport specified to the datatable provided
/// </summary>
/// <param name="u">The current Utility class</param>
/// <param name="rpt">the source report to search within</param>
/// <param name="subReportName">the name of the subreport object to find</param>
/// <param name="dataTable">the datatable to bind to your report</param>
/// <param name="hideIfEmpty">True to hide the subreport if the table is empty, false otherwise</param>
/// <returns>The report object of the subreport found</returns>
private static Telerik.Reporting.Report SetSubReport(
    Utility u,
    Telerik.Reporting.Report rpt,
    String subReportName,
    DataTable dataTable,
    Boolean hideIfEmpty)
{
    Telerik.Reporting.Report actualSubReport = null;
 
    try
    {
        if ((String.IsNullOrWhiteSpace(subReportName) == false) &&
            (rpt != null) &&
            (dataTable != null))
        {
            Telerik.Reporting.SubReport subReport = rpt.Items.Find(subReportName, true)[0] as Telerik.Reporting.SubReport;
            if (subReport != null)
            {
                Telerik.Reporting.InstanceReportSource subReportSource = subReport.ReportSource as Telerik.Reporting.InstanceReportSource;
                if (subReportSource != null)
                {
                    actualSubReport = subReportSource.ReportDocument as Telerik.Reporting.Report;
                    if (actualSubReport != null)
                    {
                        if ((hideIfEmpty == true) &&
                            ((dataTable == null) |
                             ((dataTable != null) &&
                              (dataTable.Rows.Count < 1))))
                        {
                            subReport.Visible = false;
                        }
                        else
                        {
                            Telerik.Reporting.ObjectDataSource odsSalary = actualSubReport.DataSource as Telerik.Reporting.ObjectDataSource;
                            if (odsSalary != null)
                            {
                                odsSalary.DataSource = dataTable;
                            }
                        }
                    }
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
    return actualSubReport;
}
#endregion
0
Yonggu Kang
Top achievements
Rank 1
Iron
answered on 08 Aug 2013, 02:35 AM
Dear David,

Thank you for your kind advice. It works like charm.
I read telerik doc but can't find this kind of code snippet.
Where on earth are they buried ? hard to find answer.

You save me hours. Thanks a lot.

RGDS
Kang

0
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 08 Aug 2013, 01:18 PM
Glad you found the code useful.  I too found the Telerik documentation lacking on Sub Report data binding.  With their recent changes on how things were data bound their documentation (the little that exists) points to the non-working methods.  I had to create this on my own after a bit of snooping.

One minor note which you might not have noticed.  The return value of the snipped I provided is a report document.   That allows you to do some interesting things with the return values if you need to.  I have had to set parameters directly.  I have also had to bind data to the sub-reports of the sub-reports...


0
Yonggu Kang
Top achievements
Rank 1
Iron
answered on 08 Aug 2013, 05:14 PM
Hi David,

Thank you for your additional info.
At present, I'm trying to set report view as dumb as possible and heavily relied on business object.
It just receive parms from u.i and call business object. So, I just create small static utility
with your code and call it to bind subreport with provided business object .
It took me 10 mins from start to finishing test,while I spend a dozen of hours to grasp how binding works.

If needed more sophiticated function in the furture such as subreport in subreport which needs additional parms
as  you noted,  I'll refer to your advice again. Your tip was great help for me. I appreciate it again.

Best Regards.

Kang
0
Charles
Top achievements
Rank 1
answered on 27 Jul 2014, 04:30 PM
Thanks!! Works like a champ and is very well-written. I like the ability to hide the sub if the datatable does not exist or has no data.
I found this snippet of code after about 8 hours of searching through Telerik documentation and various non-working forum posts.

They really don't discuss binding data to subreports very well.

BTW, what is the 'Utility' argument?  Is that something internal to your development process?

I'm coming from Crystal Reports in Visual Studio.  I like how they do their databinding -- you bind to whole dataset.  No need to make artificially large single tables.
0
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 27 Jul 2014, 05:44 PM
The "Utility" class is a business class written to handle logging and reporting of errors application wide, amongst other things, in an controlled standardized manner.  As it is passed into the function it is instance specific in nature.  I wrote it long ago and find it a handy shortcut in my programming.

I too have migrated away from Crystal Reports.  While I miss some of its capabilities I am still finding that I can accomplish just about everything with Telerik reports.  What I truly appreciate the most about Telerik is that they handle the 32/64 bit barrier in a manner far superior to Crystal.  I have gotten tired with Crystal reports foibles.

0
Charles
Top achievements
Rank 1
answered on 27 Jul 2014, 08:49 PM
My primary reason for moving to Telerik Reports was that I needed something that could run on Azure.  Crystal Reports also has/had - I haven't checked recently, some pretty absurd prices for doing a few occasionally used reports on a website.

At my day job, I have also started moving things to Telerik simply to avoid having to send a tech out install an msi package on each computer that needs to run a reporting application.

So far, I've found what I need to make Telerik do what I need.  It sometimes takes a lot of digging to find it though.

Thanks again for that snippet.
Tags
General Discussions
Asked by
Yonggu Kang
Top achievements
Rank 1
Iron
Answers by
David
Top achievements
Rank 1
Iron
Veteran
Iron
Yonggu Kang
Top achievements
Rank 1
Iron
Charles
Top achievements
Rank 1
Share this question
or