보고서 바인딩 및 채워진 데이터 집합으로 DataSource 설정
이 부분에서는 작성된 보고서를 인스턴스화하고 보고서의 데이터 집합을 채우고 CrystalReportViewer 컨트롤에서 보고서를 표시하는 방법에 대해 설명합니다. DataSetConfiguration 클래스의 CustomerDataSet 속성에서 반환되는 채워진 데이터 집합에 SetDataSource 속성을 할당하여 보고서를 채웁니다. 마지막으로 채워진 보고서를 CrystalReportViewer 컨트롤에 바인딩합니다.
다음과 같은 방식으로 보고서를 인스턴스화하고 바인딩할 수 있습니다.
- 포함 보고서
- 비포함 보고서
아래 절차 중 필요한 단계를 하나만 선택합니다.
비포함 보고서를 인스턴스화하고 CrystalReportViewer 컨트롤에 바인딩하려면
Web Form 또는 Windows Form을 엽니다.
보기 메뉴에서 코드를 클릭합니다.
customerReport 변수를 사용하여 ReportDocument 보고서 래퍼 클래스에 대한 새로운 클래스 수준의 선언을 추가합니다. 액세스 한정자를 private으로 설정합니다.
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\e2c9s1d7.alert_note(ko-kr,VS.90).gif" alt="Note" class="note" />참고</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>ReportDocument 클래스는 CrystalDecisions.CrystalReports.Engine 네임스페이스의 멤버입니다. 이 네임스페이스에 대한 "Imports"[Visual Basic] 또는 "using"[C#] 선언은 <a href="ms227453(v=vs.90).md">"프로젝트 설정"</a>에서 추가했습니다. ReportDocument를 인스턴스화하고 보고서를 네임스페이스에 로드하면 보고서를 포함하지 않고도 SDK를 통해 보고서에 액세스할 수 있습니다.</p></td>
</tr>
</tbody>
</table>
``` vb
Private customerReport As ReportDocument
```
``` csharp
private ReportDocument customerReport;
```
"프로젝트 설정" 과정 중 한 절차를 통해 추가한 ConfigureCrystalReports() 메서드 내에서 ReportDocument 클래스를 인스턴스화합니다.
customerReport = New ReportDocument()
customerReport = new ReportDocument();
문자열 변수를 선언하고 이름을 reportPath로 지정한 다음 이 변수에 로컬 보고서의 런타임 경로를 할당합니다. 이 경로는 웹 사이트와 Windows 프로젝트 간에 다르게 결정됩니다.
웹 사이트의 경우 로컬 보고서 파일의 이름을 문자열 매개 변수로 Server.MapPath() 메서드에 전달합니다. 그러면 런타임에 로컬 보고서가 하드 드라이브 파일 디렉터리 경로에 매핑됩니다.
Dim reportPath As String = Server.MapPath("Customer.rpt")
string reportPath = Server.MapPath("Customer.rpt");
Windows 프로젝트의 경우 백슬래시를 사용하여 Application.StartupPath 속성과 로컬 보고서 파일 이름을 연결합니다. 그러면 보고서가 Windows 실행 파일과 동일한 디렉터리에 매핑됩니다.
참고
컴파일할 때 보고서를 실행 파일이 있는 디렉터리에 복사하십시오.
Dim reportPath As String = Application.StartupPath & "\" & "Customer.rpt"
string reportPath = Application.StartupPath + "\\" + "Customer.rpt";
ReportDocument 인스턴스의 Load() 메서드를 호출하여 reportPath 문자열 변수를 전달합니다.
``` vb
customerReport.Load(reportPath)
```
``` csharp
customerReport.Load(reportPath);
```
데이터 집합을 선언하고 DataSetConfiguration.CustomerDataSet property에 할당합니다.
Dim myDataSet As DataSet = DataSetConfiguration.CustomerDataSet
DataSet dataSet = DataSetConfiguration.CustomerDataSet;
customerReport ReportDocument 인스턴스의 SetDataSource() 메서드를 호출하여 DataSet 인스턴스를 전달합니다.
``` vb
customerReport.SetDataSource(myDataSet)
```
``` csharp
customerReport.SetDataSource(dataSet);
```
보고서를 로딩하고 다음 줄에서 CrystalReportViewer의 ReportSource 속성을 ReportDocument 인스턴스에 바인딩합니다.
myCrystalReportViewer.ReportSource = customerReport
crystalReportViewer.ReportSource = customerReport;
포함 보고서를 인스턴스화하고 CrystalReportViewer 컨트롤에 바인딩하려면
Web Form 또는 Windows Form을 엽니다.
보기 메뉴에서 코드를 클릭합니다.
클래스 서명 위에 System.Data 네임스페이스에 대한 "Imports"[Visual Basic] 또는 "using"[C#] 선언이 없으면 클래스 위에 해당 선언을 추가합니다.
``` vb
Imports System.Data
```
``` csharp
using System.Data;
```
customerReport 변수 이름을 사용하여 Customer 보고서 래퍼 클래스에 대한 새로운 클래스 수준의 선언을 추가합니다. 액세스 한정자를 private으로 설정합니다.
Private customerReport As Customer
private Customer customerReport;
ConfigureCrystalReports() 메서드 내에서 보고서 래퍼 클래스를 인스턴스화합니다.
참고
ConfigureCrystalReports() 메서드는 "프로젝트 설정"에서 만들었습니다.
customerReport = New Customer()
customerReport = new Customer();
보고서 인스턴스화 코드 아래의 다음 줄에서 데이터 집합을 선언합니다.
이 단계와 다음 단계에서는 변수 선언을 변수 할당과 분리합니다. 이 자습서의 웹 사이트 관련 부록에서는 ASP.NET Cache 개체를 통해 데이터 집합을 캐싱하는 코드 블록으로 변수 할당을 리팩터링하므로 코드의 각 줄이 별도로 유지됩니다.
``` vb
Dim myDataSet As DataSet
```
``` csharp
DataSet dataSet;
```
DataSet 인스턴스를 DataSetConfiguration.CustomerDataSet 속성에 할당합니다.
myDataSet = DataSetConfiguration.CustomerDataSet
dataSet = DataSetConfiguration.CustomerDataSet;
CustomerReport 인스턴스의 SetDataSource() 메서드를 호출하여 DataSet 인스턴스를 전달합니다.
``` vb
customerReport.SetDataSource(myDataSet)
```
``` csharp
customerReport.SetDataSource(dataSet);
```
CrystalReportViewer 컨트롤의 ReportSource 속성을 CustomerReport 인스턴스에 바인딩합니다.
myCrystalReportViewer.ReportSource = customerReport
crystalReportViewer.ReportSource = customerReport;
Customer 보고서 및 채워진 데이터 집합에 대한 로드를 테스트하려면
이제 프로젝트를 빌드하고 실행할 준비가 되었습니다.
빌드 메뉴에서 솔루션 빌드를 클릭합니다.
빌드 오류가 발생하면 바로 수정합니다.
Windows 프로젝트에서 비포함 보고서를 사용하는 경우 \bin\ [Visual Basic] 또는 \bin\debug\ [C#] 하위 디렉터리에서 컴파일된 Windows 실행 파일을 찾은 다음 이 하위 디렉터리로 보고서를 복사합니다.
참고
런타임에 비포함 보고서를 Windows 실행 파일에서 로드하도록 하려면 보고서가 Windows 실행 파일과 동일한 디렉터리에 저장되어 있어야 합니다.
디버그 메뉴에서 시작을 클릭합니다.
Customer 보고서가 나타나고 데이터 집합에 배치한 채워진 데이터가 표시됩니다.
Visual Studio로 돌아간 다음 중지를 클릭하여 디버그 모드를 종료합니다.
Windows 프로젝트를 작성하는 경우 이제 자습서를 완료했습니다.
웹 사이트를 작성하는 경우 "웹 사이트에서 데이터 집합 캐싱 "으로 이동합니다.