I currently have a report with 2 subreports. At runtime I set the report's data source to a DataSet. I also set the data source for the 2 subreports to the same DataSet. No data appears to be getting to the subreports.
public PeakReport(DataSet ds)
{
/// <summary>
/// Required for telerik Reporting designer support
/// </summary>
InitializeComponent();
DataSource = ds;
subAMDaily.Report.DataSource = ds;
subPMDaily.Report.DataSource = ds;
}
13 Answers, 1 is accepted
To drive data into subreports there are three ways:
1. Using Report Filters.
2. Using NeedDataSource event handler.Please refer to the Product Line Sales Report example (Telerik | Reporting | Examples | C# Solution):
- In the report which will serve as sub report (some times named Slave Report):
- Edit DataSource property to connect report to the data;
- Edit ReportParameters property to create proper parameters;
- Edit Fileters property to create data source filtering criteria.
- In the report which will serve as master report:
- Add Subreport item and assign it's ReportSource property to the report described above;
- Edit subreport's Parameters property to bind parameters of slave report to data fields of master report.
- First preview the TopEmployees report and initialization of it's DataSource, Filters and ReportParameters properties;
- Then look how the master report (ProductLineSales) drives the TopEmployees through subreport's Parameters property.
In Master report opened in report designer select the Subreport item and create a NeedDataSource event handler. In this event handler we are initializing the sub report's DataSource property. We can use command parameters to filter the data:
Telerik.Reporting.Processing.ReportItemBase item = sender as Telerik.Reporting.Processing.ReportItemBase; |
DataRowView dataRow = (DataRowView)item.DataItem; |
string commandText = @"SELECT id, name, invoiceID FROM theTable WHERE groupID=@parameterID"; |
SqlDataAdapter adapter = new SqlDataAdapter(commandText, connectionString); |
adapter.SelectCommand.Parameters.AddWithValue("@parameterID", dataItem["InvoiceID"]); |
this.DataSource = adapter; |
where InvoiceID is real column name from master report's data source.3. Using ItemDataBinding event handler.
In already mentioned Product Line Sales Report example you can preview chart1_NeedDataSource method of ProductLineSales report where we initialized the chat's data source in same fashion.
About using one DataSet object as data source for master and sub reports. Probably the empty reports in your case are result of not filled DataSet object. Please preview attached 107491A sample solution where three-level hierarchy is displayed.
- Create Slave report and connect its DataSource through SqlDataAdapter with command parameter as demonstrated here and expose the parameter through public property;
Please preview attached 107491B sample solution.
- In Master report opened in report designer select the Subreport item and create a ItemDataBinding event handler. In this event handler we are initializing the sub report's command parameter.
Note: refer to Performance Considerations before you decide which method to implement in your case.
Please refer following help-topics for more information as well: Adding a Data Source Programmatically, Filtering Report Data, Edit Filter Dialog, Creating Master-Detail Reports Using SubReports, Display Hierarchical Data with SubReports.
I hope this information helps.
Regards,
Ivan
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
The web report then creates a DataSet from the xml file and uses that DataSet for the data source of the report.
The master report works great. But the subreports don't get any data. I will need to use report parameters and filters, but so far even without them they don't get any data. I know the xml file contains the data for the table used by the subreports. I also tried writing the DataSet back to xml after loading it from the xml file and all the data is still present. So I don't think it's a problem with the DataSet itself.
I'd like to verify that setting the subreports data source in this manner is appropriate:
subAMDaily.Report.DataSource = ds;
I tried using the NeedDataSource event, but with the same result of no data.The only suspicious thing I've noticed is that the master report fields work whether I set the DataMember property to the table name or not.
#ERROR # The expression contains object 'speed_avg' that is not defined in the current context.
I have already sent you an answer to your support ticket ("Data source for subreports"), with a working sample attached. Please check it and let me know if I can be of any help.
Regards,
Ivan
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Instead of this code:
subAMDaily.Report.DataSource = ds;
subPMDaily.Report.DataSource = ds;
I needed to do this:subAMDaily.ReportSource.DataSource = ds;
subPMDaily.ReportSource.DataSource = ds;
Thank you for the heads up. I would make sure these are included in our documentation for the next release.
Kind regards,
Steve
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Private Sub SubReport1_NeedDataSource(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubReport1.NeedDataSource |
Dim item As Telerik.Reporting.Processing.ReportItemBase = TryCast(sender, Telerik.Reporting.Processing.ReportItemBase) |
Dim dataRow As DataRowView = DirectCast(item.DataItem, DataRowView) |
Dim commandText As String = "SELECT * FROM tblERmain WHERE er_id=@er_id" |
Dim connectionString As SqlConnection = GetSqlConnection() |
Dim adapter As New SqlDataAdapter(commandText, connectionString) |
Dim testFlag As String = item.DataObject("er_id") |
adapter.SelectCommand.Parameters.AddWithValue("@er_id", item.DataItem("er_id")) |
'SubReport1.ReportSource.DataSource = adapter |
Dim dt As DataSet = New DataSet |
adapter.Fill(dt, "ERtotals") |
SubReport1.ReportSource.DataSource = dt |
Dim flag As String = "" |
End Sub |
I know that the adapter and dataset are grabbing the correct information. The problem is that when the report shows up, the subreport is not showing the information from the database. I'm not sure why. In the master report in the subreport1 properties I have the ReportSource pointing to my class library and the correct report. With this, the database info just doesn't show up. When I have the reportsource not pointing to anything, then nothing shows up, which makes sense. But what could I be doing incorrectly that the information isn't showing?
If SubReport1 is the name of the subreport item itself, then the NeedDataSource should look like this:
Telerik.Reporting.Processing.SubReport subReportItem = sender as Telerik.Reporting.Processing.SubReport;
subReportItem.InnerReport.DataSource = dt
Note that we use the processing subreport object and the InnerReport property to access the report used as subreport.
Sincerely yours,
Steve
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
I have a Report with a Subreport, both have different object DataSources; reports are displayed in an web page where DataSources are set-up. In other words I must be able to set DataSource for both reports (Master & Slave) from web-page.
How can I do that?
Thanks!
To be able to modify items in a report that serves as a sub-report, you will have to modify a bit your application. Since the introduction of the new Report Sources, the ReportSource property of theSubReport item is now of type ReportSource (not Report like before).
var subReport =
new
DetailReport();
subReport.DataSource = ...
//instantiate the master report
var masterReport =
new
MasterReport();
//set the master report's DataSource
masterReport.DataSource =
...
//find the subreport item inside the master report
var subRepItem = report.Items.Find(
typeof
(Telerik.Reporting.SubReport),
true
)[0]
as
Telerik.Reporting.SubReport;
//set it's report source to a new InstanceReportSource with the report document that you have already instantiated above
subRepItem.ReportSource =
new
InstanceReportSource { ReportDocument = subReport };
//display the master report in the viewer
this
.ReportViewer1.ReportSource =
new
InstanceReportSource { ReportDocument = masterReport };
If you are already using InstanceReportSource in the master report, then you can take the SubReportItem.ReportSource and cast it directly (instead of instantiating new DetailReport) if that works better for you.
All the best,
Elian
the Telerik team
BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >
Telerik.Reporting.Processing.ReportItemBase item = sender as Telerik.Reporting.Processing.ReportItemBase; |
DataRowView dataRow = (DataRowView)item.DataItem; |
Is there a way to accomplish this in the current version? I was unable to get this code to work so that i could get the value of cell in the current row.
Something similar is actually done here in this video at around 14:30 as well and I was unable to reproduce it.
http://tv.telerik.com/watch/reporting/video/working-with-charts-telerik-reports
the DataItem property was deprecated a couple of years ago. It has been replaced by the DataObject property.
For more info see:
Greetings,
Elian
the Telerik team
BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >