In VS2005, the report designer is great for quickly throwing together a report and binding it to an ObjectDataSource... but what if you want to do this through code for more control?
The steps are to create the report _without_ using a TableAdapter, and then manually create the data source through code (below). Note that the "rds.Name" property must be set to the "DataSetName" attribute in the report's RSDL file:
<DataSetName>DSCustomers_OutCustomers< FONT>DataSetName>
Dim dsCustomers As DSCustomers
Dim oCustomers As New CCustomers()
dsCustomers = oCustomers.GetCustomers()
Dim rds As ReportDataSource = New ReportDataSource
ReportViewer1.LocalReport.DataSources.Clear()
rds.Name = "DSCustomers_OutCustomers"
rds.Value = dsCustomers.OutCustomers
ReportViewer1.LocalReport.DataSources.Add(rds)
ReportViewer1.DataBind()
As a second example, let's say you use a Typed DataSet and TableAdapter as a Data Source and you are creating a WindowsForms application. In the example below, a Typed DataSet has been created called "DataSet1" which queries the Customers table.
Some important things to remember:
- you need to call the Reset() method on the ReportViewer whenever you dynamically switch reports.
- you need to prefix report (RDLC) filenames with the Namespace of the project the report is in (below the namespace is "ReportViewerTest.")
reportViewer1.Reset();
DataSet1TableAdapters.CustomersTableAdapter ta1 = new DataSet1TableAdapters.CustomersTableAdapter();
DataSet1.CustomersDataTable dt1 = new DataSet1.CustomersDataTable();
ta1.Fill(dt1);
ReportDataSource rds = new ReportDataSource();
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerTest.Report1.rdlc";
rds.Name = "DataSet1_Customers";
rds.Value = dt1;
reportViewer1.LocalReport.DataSources.Add(rds);
reportViewer1.RefreshReport();