2011-06-13 5 views
2

나는 행과 열을 동적으로 추가하기가 더 쉽기 때문에 데이터베이스에서 보고서를 생성하고 DataGridView에 표시하는 C# 프로젝트를 작성 중입니다. 그러나 이제는 인쇄 목적으로 DataGridView 콘텐트를 ReportViewer로 내 보내야합니다. ReportViewer를 표시하고 싶지 않습니다. 난 그냥 인스턴스를 만들고 내 DatagridView에서 채우기를 누른 다음 hight 품질 및 출력에 잘 구성된 레이아웃을 얻기 위해 즉시 인쇄 대화 상자를 표시하고 싶습니다. 현재 HTML로 변환하여 보고서를 인쇄 한 다음 WebBrowser 컨트롤에 할당하고 ShowPrintDialog() 메서드를 호출합니다. 그러나이 방법으로 WebBrowser가 테이블을 순차적으로 출력하므로 모든 페이지에서 열 머리글을 인쇄하는 것과 같은 일부 인쇄 문제를 제어 할 수 없습니다.DataGridView를 ReportViewer로 내보내려면 어떻게해야합니까?

이 프로젝트는 프로덕션 프로젝트이므로 가장 짧은 시간에이 목표에 도달해야합니다.

모든 아이디어는 높이 평가됩니다.

답변

0

당신은 마이크로 소프트 보고서 뷰어 http://www.microsoft.com/en-us/download/details.aspx?id=3841 프로젝트에 DLL을 추가 사용해야합니다 Microsoft.ReportViewer.Common 및 Microsoft.ReportViewer.WinForms 당신이 가진 * .rdlc 파일을 작성해야합니다 있도록 디자인을 변경할 수 있습니다 귀하의 보고서. 마지막으로 저장하려면 다음 중 하나를 수행하십시오.

public static void PrintArticles(ICollectionView Articles) 
     { 

      try 
      { 
       var articlesRows = new DataTable("Articles"); 
       articlesRows.Columns.Add("Id"); 
       articlesRows.Columns.Add("Description"); 

       var arts = Articles.Cast<Article>(); 

       foreach (var art in arts) 
       { 
        articlesRows.Rows.Add(art.Id, art.Description); 
       } 

       ReportViewer reporter = new ReportViewer(); 
       reporter.LocalReport.DisplayName = "Report1"; 
       reporter.LocalReport.ReportPath = Path.Combine(Program.BasePath + "PrintArticles.rdlc"); 
       reporter.LocalReport.DataSources.Clear(); 
       reporter.LocalReport.DataSources.Add(new ReportDataSource("Project1", articlesRows)); 

       byte[] report = reporter.LocalReport.Render("PDF"); 

       reporter.LocalReport.ReleaseSandboxAppDomain(); 

       string pdfpath = Path.Combine(Program.BasePath, "file.pdf"); 

       if (File.Exists(pdfpath)) 
        File.Delete(Path.Combine(Program.BasePath, "file.pdf")); 

       FileStream writer = new FileStream(pdfpath, FileMode.Create); 
       writer.Write(report, 0, report.Length); 
       writer.Close(); 

       Process ar = Process.Start(pdfpath); 
      } 
      catch (Exception e) 
      { 

      } 
      } 
관련 문제