I'm trying to achieve something in a report, and I'm getting nowhere and beginning to wonder whether I'm doing this all wrong.
As a data source for my report, I have a CLR object; It has the following structure (specifically the Bundle object):
public class Bundle
{
public uint BatchId { get; set; }
public uint BundleId { get; set; }
public List<BundleItem> Items { get; set; } = new List<BundleItem>();
}
public class BundleItem
{
public uint CheckNo { get; set; }
public string ReceivedFrom { get; set; }
public string Bank { get; set; }
public string Memo { get; set; }
public decimal Amount { get; set; }
public ushort AccountNo { get; set; }
public string AccountDescription { get; set; }
#if NET6_0
public DateOnly ContribDate {get;set;}
#else
public string ContribDate { get; set; }
#endif
public ushort PaymentType { get; set; }
public DateTime EntryDateTime { get; set; }
}
I'm able to instantiate that object, and want to pass it off to a report to render; it's a complicated scenario regarding how that CLR object is instantiated, so I'm not able to use something like a SqlDataSource, so I'm assuming the correct data source type to use is an objectDataSource. That side of things so far is working (as best as I can tell):
I need to build two different reports from this data: one that aggregates the BundleItems (contained in the Items property), and one that displays the list of BundleItems and their fields. I can drag-drop [= Fields.BatchId] and [= Fields.BundleId] to the report designer (I'm using VS integrated designer) and that works fine. I'm binding the datasource to the report via:
BundleReport report = new ();
report.DataSource = (object)_detailsList;
ReportViewer1.ReportSource = report;
ReportViewer1.ViewMode = ViewMode.PrintPreview;
ReportViewer1.RefreshReport();
That causes the report to open and display successfully. Success so far!
The problems I'm having are related to the List<BundleItems>; no matter what I do the ReportViewer is just (I'm guessing) calling .ToString() on the list and getting back the type name. This has left me to wonder if I'm trying to do something that's just not supported; is there a way for me to visualize a List<BundleItem> in a report directly? Does it have to be done via a sub report? I'm feeling lost - and wondering if I'm just trying to achieve something that it's not really designed for (I know the old ReportViewer WinForm control hated working with anything other than DataSets).
Any guidance is greatly appreciated!
Hi Joshua,
To display the Items fields, you can add a List to the report. Then set its Binding property as follows:
Property path: DataSoure
Expression: =Fields.Items
Then, in the List, you need to add textboxes and manually type the name of the fields. For example "=Fields.ReceivedFrom"
You can find more details in the How to Databind to Collection Properties KB article.