Wednesday, June 13, 2007

ASP.NET 2.0 ReportViewer :: Continued...

I have one report viewer wired to two different local reports and users can select the report from a dropdownlist. Here's the code to show the correct report:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// remove previous report viewer if it exists
if (Panel1.Controls.Count > 0)
{
Panel1.Controls.Clear();
}

// create new instance of reportviewer to get around lame bug
ReportViewer rv = new ReportViewer();
ReportDataSource rds = new ReportDataSource("dsCustodians_Custodian", this.objDSRptCustodians);
rv.LocalReport.DataSources.Add(rds);
rv.Width = new Unit("100%");
rv.Height = new Unit("400px");

// set the reportpath dynamically
if (this.DropDownList1.SelectedValue == "Custodians")
{
rv.LocalReport.ReportPath = Server.MapPath("rpt_GroupedByCustodians.rdlc");
}
else if (this.DropDownList1.SelectedValue == "Departments")
{
rv.LocalReport.ReportPath = Server.MapPath("rpt_GroupedByDepartment.rdlc");
}

// add the dynamically called report to the panel
Panel1.Controls.Add(rv);
}



-Eurlist.thesol.com

2 comments:

Anonymous said...

Thanks for your post. My problem is that my reportviewer alway is shown above some server controls. How can i set the reportviewer's position.

Thanks in advance.

Unknown said...

The ReportViewer is just another control and you should be able to set it's position like any other control.

I ended up putting the ReportViewer in a panel control and manipulated the position of the panel in my .aspx page i.e. "Panel1.Controls.Add(ReportViewer1);" This is also how I am able to switch between various reports on the fly.

Also remember to take a look at the ReportViewer's properties like width, horizontalalign, height, etc.

Eurlist.thesol.com