ASP.NET - RDLC Reports: Dynamically adding DrillThrough event in ReportViewer

Asked By Manu on 10-Oct-11 08:45 AM
Hi,

How can I add DrillThrough event in runtime in ReportViewer.
Is there any way to achieve the same.

Thanks.
Suchit shah replied to Manu on 10-Oct-11 08:57 AM
ReportViewer.Drillthrough Event

Occurs when a drillthrough item is selected

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;

public partial class _Default : System.Web.UI.Page
{
      private DataTable LoadEmployeesData()
      {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(@"c:\My Reports\employees.xml");
        return dataSet.Tables[0];
      }

      private DataTable LoadDepartmentsData()
      {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(@"c:\My Reports\departments.xml");
        return dataSet.Tables[0];
      }

      void DemoDrillthroughEventHandler(object sender, DrillthroughEventArgs e)
      {
        LocalReport localReport = (LocalReport)e.Report;
        localReport.DataSources.Add(new ReportDataSource("Employees",
          LoadEmployeesData()));
      }

    protected void Page_Init(object sender, EventArgs e)
    {

      if (!IsPostBack)
        {
        // On initial page load
        ReportViewer1.LocalReport.ReportPath = @"c:\My Reports\Departments.rdlc";

        // Supply a DataTable corresponding to each report data source.
        ReportViewer1.LocalReport.DataSources.Add(
          new ReportDataSource("Departments", LoadDepartmentsData()));
        }

      // Add the handler for drillthrough.
      ReportViewer1.Drillthrough += new DrillthroughEventHandler(DemoDrillthroughEventHandler);

    }
}
Suchit shah replied to Manu on 10-Oct-11 08:58 AM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;
using Microsoft.Reporting.WebForms;

public partial class DrillThroughReport_Parent : System.Web.UI.Page
{
    public string thisConnectionString =
    ConfigurationManager.ConnectionStrings
    ["NorthwindConnectionString"].ConnectionString;
    public string thatConnectionString =
    ConfigurationManager.ConnectionStrings
    ["NorthwindConnectionString"].ConnectionString;
    public SqlParameter[] Level1SearchValue = new SqlParameter[1];
    public SqlParameter[] Level2SearchValue = new SqlParameter[1];

    protected void RunReportButton_Click(object sender, EventArgs e)
    {
      ReportViewer1.Visible = true;

      SqlConnection thisConnection = new SqlConnection(thisConnectionString);
      System.Data.DataSet thisDataSet = new System.Data.DataSet();

      //Run the stored procedure  to fill dataset for Parent.rdlc

      thisDataSet = SqlHelper.ExecuteDataset(thisConnection,
           "List_Customers_OrderTotal");

      //Assign dataset to report datasource

      ReportDataSource datasource =
      new ReportDataSource("DrillThroughDataSet_ListCustomersOrderTotal",
      thisDataSet.Tables[0]);

      //Assign datasource to reportviewer control

      ReportViewer1.LocalReport.DataSources.Clear();
      ReportViewer1.LocalReport.DataSources.Add(datasource);
      ReportViewer1.LocalReport.Refresh();
    }

    protected void ReportViewer1_Drillthrough(object sender,
        DrillthroughEventArgs e)
    {
      //Get OrderID that was clicked by

      //user via e.Report.GetParameters()

      ReportParameterInfoCollection DrillThroughValues =
                 e.Report.GetParameters();   

      //This is just to show you how to iterate

      //through the collection if you have

      //multiple parameters values instead of a single parameter value.

      //To process multiple parameters values,

      //concatenate d.Values[0] into a string with a delimiter.

      //Use the Split() method to  separate values

      //into an array. Assign indivdual array element to

      //corresponding parameter array element.

      foreach (ReportParameterInfo d in DrillThroughValues)
      {
        lblParameter.Text = d.Values[0].ToString().Trim();
      }
      LocalReport localreport = (LocalReport)e.Report;

      //Fill dataset for Level1.rdlc

      SqlConnection thisConnection = new SqlConnection(thisConnectionString);
      System.Data.DataSet Level1DataSet = new System.Data.DataSet();

      Level1SearchValue[0] = new SqlParameter("@OrderID",
                 lblParameter.Text.Trim());
      Level1DataSet = SqlHelper.ExecuteDataset(thisConnection,
             "Show_OrderDetails", Level1SearchValue);

      ReportDataSource level1datasource = new
      ReportDataSource("DrillThroughDataSet_ShowOrderDetails",
      Level1DataSet.Tables[0]);
      localreport.DataSources.Clear();
      localreport.DataSources.Add(level1datasource);
      localreport.Refresh();

      //Fill dataset for Level2.rdlc.

      SqlConnection thatConnection =
      new SqlConnection(thatConnectionString);
      System.Data.DataSet Level2DataSet = new System.Data.DataSet();

      Level2SearchValue[0] =
      new SqlParameter("@ProductID", lblParameter.Text);
      Level2DataSet = SqlHelper.ExecuteDataset(thisConnection,
             "Show_Products", Level2SearchValue);

      ReportDataSource level2datasource =
      new ReportDataSource("DrillThroughDataSet_ShowProducts",
      Level2DataSet.Tables[0]);
      //No need to clear datasource again

      localreport.DataSources.Add(level2datasource);
      localreport.Refresh();
    }   
}

http://www.codeproject.com/KB/aspnet/DrillThroughReport.aspx
dipa ahuja replied to Manu on 10-Oct-11 02:34 PM
The user can Navigate to another report by clicking a link. This will fire the ReportViewer.Drilltrhough event on a ReportViewer control in ASP .NET 2.0. The DrillthroughEventArgs contains the new ReportPath. With this information you can create navigate to the correct URL on you’re ASP .NET 2.0 website.

protected void ReportViewer1_Drillthrough(object sender, Microsoft.Reporting.WebForms.DrillthroughEventArgs e)
{
Response.Redirect(string.Format("Test.aspx?reportPath={0}", e.ReportPath));
}