8단원: 데이터 필터 만들기

부모 보고서에 드릴스루 동작을 추가한 후에는 자식 보고서에 대해 정의한 데이터 테이블에 대한 데이터 필터를 만듭니다.

드릴스루 보고서에 대한 테이블 기반 필터 또는 쿼리 필터를 만들 수 있습니다. 이 단원에서는 두 옵션에 대한 지침을 제공합니다.

테이블 기반 필터

테이블 기반 필터를 구현하려면 다음 작업을 완료해야 합니다.

  • 자식 보고서의 테이블릭스에 필터 식을 추가합니다.

  • PurchaseOrderDetail 테이블에서 필터링되지 않은 데이터를 선택하는 함수를 만듭니다.

  • 자식 보고서에 PurchaseOrderDetail DataTable 을 바인딩하는 이벤트 처리기를 추가합니다.

자식 보고서의 테이블릭스에 필터 식 추가

  1. 자식 보고서를 엽니다.

  2. 테이블릭스에서 열 머리글을 선택하고 열 머리글 위에 나타나는 회색 셀을 마우스 오른쪽 단추로 클릭한 다음 테이블릭스 속성을 선택합니다.

  3. 필터 페이지를 선택한 다음 추가를 선택합니다.

  4. 제출된 식드롭다운 목록에서 ProductID를 선택합니다. 이 열은 필터를 적용하는 위치입니다.

  5. 연산자 드롭다운 목록에서 등호(=) 연산자를 선택합니다.

  6. 값 필드 옆에 있는 식 단추를 선택하고 범주 영역에서 매개 변수선택한 다음 값 영역에서 productid두 번 클릭합니다. 값 필드에 대한 Set 식은 이제 =Parameters!productid와 유사한 식을 포함해야 합니다. 값입니다.

  7. 테이블릭스 속성 대화 상자에서 확인을 선택하고 확인을 다시 선택합니다.

  8. .rdlc 파일을 저장합니다.

PurchaseOrdeDetail 테이블에서 필터링되지 않은 데이터를 선택하는 함수 만들기

  1. 솔루션 탐색기 Default.aspx를 확장한 다음 Default.aspx.cs를 두 번 클릭합니다.

  2. 정수 형식의 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 테이블에서 필터링된 데이터를 선택한 함수를 만듭니다 .

  • 매개 변수 값을 검색하고 PurchaseOrdeDetail DataTable을 자식 보고서에 바인딩하는 이벤트 처리기를 추가합니다.

PurchaseOrderDetail 테이블에서 필터링된 데이터를 선택하는 함수 만들기

  1. 솔루션 탐색기 Default.aspx를 확장한 다음 Default.aspx.cs를 두 번 클릭합니다.

  2. 정수 형식의 productid 매개 변수를 수락하고 데이터 처리 가능한 개체를 반환하고 다음을 수행하는 새 함수를 만듭니다.

    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;  
              }  
          }  
      

매개 변수 값을 검색하고 PurchaseOrdeDetail 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. 파일을 저장합니다.

다음 단계

자식 보고서에 대해 정의한 데이터 테이블에 대한 데이터 필터를 만들었습니다. 다음으로 웹 사이트 애플리케이션을 빌드하고 실행합니다. 9단원: 애플리케이션 빌드 및 실행 참조