65.9K
CodeProject 正在变化。 阅读更多。
Home

仅使用数据集动态创建 RDLC 报表

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.83/5 (4投票s)

2015年3月19日

CPOL

1分钟阅读

viewsIcon

69997

downloadIcon

2621

仅通过绑定数据集来创建 RDLC 报表

引言

为了使用 ReportViewer 控件显示报表,我们需要一个 DataSource 包含要显示的数据,以及一个 Report 文档描述如何显示这些数据。

本技巧介绍如何使用 DataSet 在 Report 文档 (*.rdlc) 中动态创建 Report ,并毫不费力地显示报表。

在之前的搜索中,显示数据需要创建报表并绑定 dataset。在这里,我提供了一种通过传递 DataSet 在 RDLC 中生成报表的方法。

我目前需要一个能够轻松显示报表的组件,因为这些报表可以轻松地应用于系统维护表,并且您不会在这些系统维护报表上花费太多时间。

背景

要使用此报表,您不需要了解 RDLC,即使是初学者或新手也可以使用此报表生成器。

Using the Code

在您的代码中使用方法

首先,您需要将 ReportBuilder.cs ReportBuilderEngine.cs 复制到您的项目文件中。这些文件用于从您的 Dataset 生成报表。

其次,您需要将 UserControl 文件夹复制到您的解决方案中。它包含 report_viewer.ascx 文件,该文件实际上生成报表。如果您需要更改任何页脚详细信息,可以在 report_viewer.ascx 中进行更改。

public void DataBind(DataSet ds)
  {
  int count = 0;
  foreach (DataTable dt in ds.Tables)
  {
   count++;
   var report_name = "Report" + count;
   DataTable dt1 = new DataTable(report_name.ToString());
   dt1 = ds.Tables[count - 1];
   dt1.TableName = report_name.ToString();
  }

  //Report Viewer, Builder and Engine
       
 ReportViewer1.Reset();
 for (int i = 0; i < ds.Tables.Count; i++)
   ReportViewer1.LocalReport.DataSources.Add(
      new ReportDataSource(ds.Tables[i].TableName,ds.Tables[i]));

 ReportBuilder reportBuilder = new ReportBuilder();
 reportBuilder.DataSource = ds;
 reportBuilder.Page = new ReportPage();
 ReportSections reportFooter = new ReportSections();
 ReportItems reportFooterItems = new ReportItems();
 ReportTextBoxControl[] footerTxt = new ReportTextBoxControl[3];
 string footer = string.Format
 ("Copyright  {0}         Report Generated On  {1}          Page  {2}  of {3} ", 
 DateTime.Now.Year, DateTime.Now, ReportGlobalParameters.CurrentPageNumber, 
ReportGlobalParameters.TotalPages);
 footerTxt[0] = new ReportTextBoxControl() 
 { Name = "txtCopyright", ValueOrExpression = new string[] { footer } }
 reportFooterItems.TextBoxControls = footerTxt;
 reportFooter.ReportControlItems = reportFooterItems;
 reportBuilder.Page.ReportFooter = reportFooter;
 ReportSections reportHeader = new ReportSections();
 reportHeader.Size = new ReportScale();
 reportHeader.Size.Height = 0.56849;
 ReportItems reportHeaderItems = new ReportItems();
 ReportTextBoxControl[] headerTxt = new ReportTextBoxControl[1];
 headerTxt[0] = new ReportTextBoxControl() { Name = "txtReportTitle", 
 ValueOrExpression = new string[] { "Report Name: "+ReportTitle } };
 reportHeaderItems.TextBoxControls = headerTxt;
 reportHeader.ReportControlItems = reportHeaderItems;
 reportBuilder.Page.ReportHeader = reportHeader;
 ReportViewer1.LocalReport.LoadReportDefinition(ReportEngine.GenerateReport(reportBuilder));
 ReportViewer1.LocalReport.DisplayName = ReportName;
 }

您可以在以 footerTxt 开头的行中更改页脚内容。

第三,您需要将 img 文件夹复制到解决方案中,并将 logo.png 替换为您的徽标。您需要将您的徽标重命名为 "logo.png"。

如何将数据集与报表绑定?

public void ReportBinding()
{
  //Data for binding to the Report

  DataTable table1 = new DataTable("patients");
  table1.Columns.Add("Name");
  table1.Columns.Add("State");
  table1.Columns.Add("Country");
  table1.Columns.Add("CurrentResidence");
  table1.Columns.Add("MaritalStatus");
  table1.Columns.Add("EmploymentStatus");
  table1.Rows.Add("Nadir", "Kerala", "India", 
  "Bangalore", "Single","Is SelfEmployed");
  table1.Rows.Add("Lijo", "Kerala", "India", 
  "Philipines", "Single", "Is Salaried");
  table1.Rows.Add("Shelley", "Kerala", "India", 
  "Kashmir", "Married", "Is SelfEmployed");
  DataSet ds = new DataSet();
  ds.Tables.Add(table1);

  //Report Binding

  rpt_daily.ReportTitle = "Wastage Report";
  rpt_daily.ReportName = "WastageReport";
  rpt_daily.DataBind(ds);
  rpt_daily.Visible = true; 
}

历史

  • 演示版本 1.0
© . All rights reserved.