レッスン 8: データ フィルターを作成する

親レポートにドリルスルー アクションを追加した後は、子レポート用に定義したデータ テーブル用のデータ フィルターを作成します。

詳細レポートに対しては、テーブルベースのフィルター または クエリ フィルターを作成できます。 このレッスンでは、両方のオプションの手順を説明します。

テーブル ベースのフィルター

テーブルベースのフィルターを実装するには、次の操作を実行する必要があります。

  • 子レポートの Tablix にフィルター式を追加します。

  • PurchaseOrderDetail テーブルからフィルター選択されていないデータを選択する関数を作成します。

  • 子レポートに PurchaseOrderDetail DataTable をバインドするイベント ハンドラーを追加します。

子レポート内の Tablix にフィルター式を追加する

  1. 子レポートを開きます。

  2. Tablix の列見出しを選択します。列見出しの上の灰色のセルを右クリックし、 [Tablix のプロパティ] を選択します。

  3. [フィルター] ページを選択して、[追加] を選択します。

  4. [式] フィールドで、一覧から [ProductID] を選択します。 この列は、ユーザーがフィルターを適用する列となります。

  5. = [演算子] ドロップダウン リストで、等号演算子 ( ) を選択します。

  6. [値] フィールドの横の式ボタンを選択します。 [カテゴリ] 領域の [パラメーター] を選択し、 [値] 領域の [productid] をダブルクリックします。 [式の設定: 値] フィールドに、 =Parameters!productid.Value のような式が表示されます。

  7. [OK] を選択します。 [Tablix のプロパティ] ダイアログ ボックスで再び [OK] をクリックします。

  8. .rdlc ファイルを保存します。

PurchaseOrderDetail テーブルからフィルター選択されていないデータを選択する関数を作成する

  1. ソリューション エクスプローラーで、Default.aspx を展開し、Default.aspx.cs をダブルクリックします。

  2. 整数 (INTEGER) 型の productid というパラメーターを受け取る新しい関数を作成します。 これは、データテーブル オブジェクトを返して、次の操作を行います。

    1. レッスン 4: 子レポートのデータ接続とデータ テーブルを定義する」の手順 2 で作成したデータセットである DataSet2のインスタンスを作成します。

    2. SqlServer データベースへの接続を作成し、「レッスン 4: 子レポートのデータ接続とデータ テーブルを定義する」で定義されたクエリを実行します。

    3. フィルター選択されていないデータがクエリから返されます。

    4. クエリを実行して、フィルター選択されていないデータを DataSet インスタンスに入力します。

    5. PurchaseOrderDetail DataTable を返します。

      関数は次の例のようになります (この例は単なる参考です。任意のパターンに従って、子レポートに必要なデータをフェッチできます)。

      /// <summary>  
          /// Function to query PurchaseOrderDetail table, fetch the  
          /// unfiltered data and bind it with the Child report  
          /// </summary>  
          /// <returns>A dataTable of type PurchaseOrderDetail</returns>  
          private DataTable GetPurchaseOrderDetail()  
          {  
              try  
              {  
                  //Create the instance for the typed dataset, DataSet2 which will   
                  //hold the [PurchaseOrderDetail] table details.  
                  //The dataset was created as part of the tutorial in Step 4.  
                  DataSet2 ds = new DataSet2();  
      
                  //Create a SQL Connection to the AdventureWorks2008 database using Windows Authentication.  
                  using (SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks2022;Integrated Security=SSPI"))  
                  {  
                      //Building the dynamic query with the parameter ProductID.  
                      SqlDataAdapter adap = new SqlDataAdapter("SELECT PurchaseOrderID, PurchaseOrderDetailID, OrderQty, ProductID, ReceivedQty, RejectedQty, StockedQty FROM Purchasing.PurchaseOrderDetail ", sqlconn);  
                      //Executing the QUERY and fill the dataset with the PurchaseOrderDetail table data.  
                      adap.Fill(ds, "PurchaseOrderDetail");  
                  }  
      
                  //Return the PurchaseOrderDetail table for the Report Data Source.  
                  return ds.PurchaseOrderDetail;  
              }  
              catch  
              {  
                  throw;  
              }  
          }  
      

子レポートに PurchaseOrderDetail DataTable をバインドするイベント ハンドラーを追加します。

  1. デザイナー ビューで Default.aspx を開きます。

  2. ReportViewer コントロールを右クリックし、 [プロパティ] を選択します。

  3. [プロパティ] ページで、 [イベント] アイコンを選択します。

  4. [ドリルスルー] イベントをダブルクリックします。

    次のブロックのようなイベント ハンドラー セクションが、コードに追加されます。

    protected void ReportViewer1_Drillthrough(object sender, Microsoft.Reporting.WebForms.DrillthroughEventArgs e)  
    {  
    }  
    
  5. イベント ハンドラーを完成させます。 このイベント ハンドラーには、次の機能を含めます。

    1. DrillthroughEventArgs パラメーターから子レポート オブジェクト参照をフェッチする。

    2. GetPurchaseOrderDetail関数を呼び出す。

    3. PurchaseOrderDetail DataTable をレポートの対応するデータ ソースにバインドする。

      完全なイベント ハンドラーのコードは、次の例のような外見になります。

      protected void ReportViewer1_Drillthrough(object sender, Microsoft.Reporting.WebForms.DrillthroughEventArgs e)  
          {  
              try  
              {  
                   //Get the instance of the Target report.  
                  LocalReport report = (LocalReport)e.Report;  
      
                   //Binding the DataTable to the Child report dataset.  
                  //The name DataSet1 which can be located from,   
                  //Go to Design view of Child.rdlc, Click View menu -> Report Data  
                  //You'll see this name under DataSet2.  
                  report.DataSources.Add(new ReportDataSource("DataSet1", GetPurchaseOrderDetail()));  
              }  
              catch (Exception ex)  
              {  
                  Response.Write(ex.Message);  
              }  
          }  
      
  6. ファイルを保存します。

クエリ フィルター

クエリ フィルターを実装するには、次の操作を実行する必要があります。

  • PurchaseOrderDetail テーブルからフィルター選択されたデータを選択する関数を作成します。

  • パラメーター値を取得し、 PurchaseOrderDetail DataTable を子レポートにバインドするイベント ハンドラーを追加します。

PurchaseOrderDetail テーブルからフィルター選択されたデータを選択する関数を作成する

  1. ソリューション エクスプローラーで、Default.aspx を展開し、Default.aspx.cs をダブルクリックします。

  2. 新しい関数を作成します。この関数は、Integer 型の productidパラメーターを受け取った後、 datatable オブジェクトを返し、次の操作を行います。

    1. レッスン 4: 子レポートのデータ接続とデータ テーブルを定義する」の手順 2 で作成したデータセット DataSet2のインスタンスを作成します。

    2. SqlServer データベースへの接続を作成し、「レッスン 4: 子レポートのデータ接続とデータ テーブルを定義する」で定義されたクエリを実行します。

    3. このクエリには、返されるデータを親レポートで選択された ProductIDに基づいてフィルター選択するための productid パラメーターが含まれます。

    4. クエリを実行して、フィルター選択されたデータを DataSet インスタンスに入力します。

    5. PurchaseOrderDetail DataTable を返します。

      関数は次の例のようになります (この例は単なる参考です。任意のパターンに従って、子レポートに必要なデータをフェッチできます)。

      /// <summary>  
          /// Function to query PurchaseOrderDetail table and filter the  
          /// data for a specific ProductID selected in the Parent report.  
          /// </summary>  
          /// <param name="productid">Parameter passed from the Parent report to filter data.</param>  
          /// <returns>A dataTable of type PurchaseOrderDetail</returns>  
          private DataTable GetPurchaseOrderDetail(int productid)  
          {  
              try  
              {  
                  //Create the instance for the typed dataset, DataSet2 which will   
                  //hold the [PurchaseOrderDetail] table details.  
                  //The dataset was created as part of the tutorial in Step 4.  
                  DataSet2 ds = new DataSet2();  
      
                  //Create a SQL Connection to the AdventureWorks2008 database using Windows Authentication.  
                  using (SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorks2022;Integrated Security=SSPI"))  
                  {  
                      //Building the dynamic query with the parameter ProductID.  
                      SqlCommand cmd = new SqlCommand("SELECT PurchaseOrderID, PurchaseOrderDetailID, OrderQty, ProductID, ReceivedQty, RejectedQty, StockedQty FROM Purchasing.PurchaseOrderDetail where ProductID = @ProductID", sqlconn);  
      
                      // Sets the productid parameter.  
                      cmd.Parameters.Add((new SqlParameter("@ProductID", SqlDbType.Int)).Value = productid);  
      
                      SqlDataAdapter adap = new SqlDataAdapter(cmd);  
                      //Executing the QUERY and fill the dataset with the PurchaseOrderDetail table data.  
                      adap.Fill(ds, "PurchaseOrderDetail");  
                  }  
      
                  //Return the PurchaseOrderDetail table for the Report Data Source.  
                  return ds.PurchaseOrderDetail;  
              }  
              catch  
              {  
                  throw;  
              }  
          }  
      

パラメーター値を取得し、 PurchaseOrderDetail DataTable を子レポートにバインドするイベント ハンドラーを追加します。

  1. デザイナー ビューで Default.aspx を開きます。

  2. ReportViewer コントロールを右クリックし、 [プロパティ] を選択します。

  3. [プロパティ] ペインで、 [イベント] アイコンを選択します。

  4. [ドリルスルー] イベントをダブルクリックします。

    このアクションにより、次の例のようなイベント ハンドラー セクションがコードに追加されます。

    protected void ReportViewer1_Drillthrough(object sender, Microsoft.Reporting.WebForms.DrillthroughEventArgs e)  
    {  
    }  
    
  5. イベント ハンドラーを完成させます。 このイベント ハンドラーには、次の機能を含めます。

    1. DrillthroughEventArgs パラメーターから子レポート オブジェクト参照をフェッチする。

    2. フェッチした子レポート オブジェクトから子レポート パラメーター リストを取得する。

    3. パラメーターのコレクションに対して反復処理を行い、親レポートから渡された ProductIDパラメーターの値を取得する。

    4. 関数 GetPurchaseOrderDetailを呼び出して、パラメーター ProductIDの値を渡します。

    5. PurchaseOrderDetail DataTable をレポートの対応するデータ ソースにバインドする。

      完全なイベント ハンドラーのコードは、次の例のような外見になります。

      protected void ReportViewer1_Drillthrough(object sender, Microsoft.Reporting.WebForms.DrillthroughEventArgs e)  
          {  
              try  
              {  
                  //Variable to store the parameter value passed from the MainReport.  
                  int productid = 0;  
      
                  //Get the instance of the Target report.  
                  LocalReport report = (LocalReport)e.Report;  
      
                  //Get all the parameters passed from the main report to the target report.  
                  //OriginalParametersToDrillthrough actually returns a Generic list of   
                  //type ReportParameter.  
                  IList<ReportParameter> list = report.OriginalParametersToDrillthrough;  
      
                  //Parse through each parameters to fetch the values passed along with them.  
                  foreach (ReportParameter param in list)  
                  {  
                      //Since we know the report has only one parameter and it is not a multivalued,   
                      //we can directly fetch the first value from the Values array.  
                      productid = Convert.ToInt32(param.Values[0].ToString());  
                  }  
      
                  //Binding the DataTable to the Child report dataset.  
                  //The name DataSet1 which can be located from,   
                  //Go to Design view of Child.rdlc, Click View menu -> Report Data  
                  //You'll see this name under DataSet2.  
                  report.DataSources.Add(new ReportDataSource("DataSet1", GetPurchaseOrderDetail(productid)));  
              }  
              catch (Exception ex)  
              {  
                  Response.Write(ex.Message);  
              }  
          }  
      
  6. ファイルを保存します。

次のステップ

これで、子レポートに対して定義したデータ テーブルのデータ フィルターを作成できました。 次は、Web サイト アプリケーションをビルドして実行します。 「レッスン 9: アプリケーションをビルドして実行する」を参照してください。