C#的Winform - 扩展的解决方案为单一数据集,为的ReportViewer多个数据集的工作

2019-09-29 18:43发布

我从采取了这一代码礼Aghaei的解决方案 ,这已经帮我解决了在使用微软的WinForms ReportViewer控件单一的DataSet的问题。

工作部分:

调用形式:

string sql = "SELECT bk_book_details.id, bk_book_details.book_id, bk_book_details.book_no, bk_book_details.book_name, bk_book_details.edition_id, bk_book_details.condition_id, bk_book_details.publication_year, bk_book_details.price, bk_book_details.purchase_price, bk_book_details.reference_no, bk_book_details.book_status, bk_book_details.purchase_id, bk_book_details.purchase_date FROM bk_book_details";
string reportLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Reports\LMS_Book_Price_Invoice.rdl";
var f = new ReportForm();
f.ReportPath = reportLocation;
DataTable dt = (DataTable)DataAdapter.Current.LoadData(sql, "LoadDataTable");
List<DataTable> lDt = new List<DataTable>();
lDt.Add(dt);
f.ReportData = new List<DataTable>(lDt);
f.ShowDialog();

报表:

public List<DataTable> ReportData { get; set; }
public string ReportPath { get; set; }

private void ReportForm_Load(object sender, EventArgs e) {
    long numberOfDataSets = this.ReportData.Count();
    if (numberOfDataSets == 0)
        return;

    var rds = new Microsoft.Reporting.WinForms.ReportDataSource("Data", this.ReportData[i]);
    this.reportViewer1.LocalReport.DataSources.Add(rds);

    reportViewer1.LocalReport.ReportPath = this.ReportPath;
    this.reportViewer1.RefreshReport();
}

我扩展,以多个数据集,像这样:

  1. 增加了相同的SQL语句3次,除了where条件3个表添加到RDL文件有3个独立的数据集分别是:数据,数据1,数据2

修改后的代码:

调用形式:

        string sql = "SELECT bk_book_details.id, bk_book_details.book_id, bk_book_details.book_no, bk_book_details.book_name, bk_book_details.edition_id, bk_book_details.condition_id, bk_book_details.publication_year, bk_book_details.price, bk_book_details.purchase_price, bk_book_details.reference_no, bk_book_details.book_status, bk_book_details.purchase_id, bk_book_details.purchase_date FROM bk_book_details WHERE bk_book_details.book_id<=2";
        string sql1 = "SELECT bk_book_details.id, bk_book_details.book_id, bk_book_details.book_no, bk_book_details.book_name, bk_book_details.edition_id, bk_book_details.condition_id, bk_book_details.publication_year, bk_book_details.price, bk_book_details.purchase_price, bk_book_details.reference_no, bk_book_details.book_status, bk_book_details.purchase_id, bk_book_details.purchase_date FROM bk_book_details WHERE bk_book_details.book_id=4";
        string sql2 = "SELECT bk_book_details.id, bk_book_details.book_id, bk_book_details.book_no, bk_book_details.book_name, bk_book_details.edition_id, bk_book_details.condition_id, bk_book_details.publication_year, bk_book_details.price, bk_book_details.purchase_price, bk_book_details.reference_no, bk_book_details.book_status, bk_book_details.purchase_id, bk_book_details.purchase_date FROM bk_book_details WHERE bk_book_details.book_id=5";

        string reportLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Reports\LMS_Book_Price_Invoice_2.rdl";

        var f = new ReportForm();
        f.ReportPath = reportLocation;
        DataTable dt = (DataTable)DataAdapter.Current.LoadData(sql, "LoadDataTable");
        DataTable dt1 = (DataTable)DataAdapter.Current.LoadData(sql1, "LoadDataTable1");
        DataTable dt2 = (DataTable)DataAdapter.Current.LoadData(sql2, "LoadDataTable2");

        List<DataTable> lDt = new List<DataTable>();
        lDt.Add(dt);
        lDt.Add(dt1);
        lDt.Add(dt2); 
        f.ReportData = new List<DataTable>(lDt);
        f.ShowDialog();

报表:

    public List<DataTable> ReportData { get; set; }
    public string ReportPath { get; set; }

    private void ReportForm_Load(object sender, EventArgs e) {
        long numberOfDataSets = this.ReportData.Count();
        if (numberOfDataSets == 0)
            return;

        this.reportViewer1.LocalReport.DataSources.Clear();
        string dataX = "Data";
        for (int i = 0 ; i < numberOfDataSets; i++) {
            string DataName = dataX;
            if (i != 0)
                DataName += Convert.ToString(i);
            /*For our case the DataName is used to provide DataSet name Data, Data1, Data2*/

            var rds = new Microsoft.Reporting.WinForms.ReportDataSource(DataName, this.ReportData[i]);
            this.reportViewer1.LocalReport.DataSources.Add(rds);
        }
        reportViewer1.LocalReport.ReportPath = this.ReportPath;
        this.reportViewer1.RefreshReport();
    }

在此之后,代码继续为单一数据集工作,像这样:

然而,对于多个数据集,它提供了以下错误:

搜索结果:

  1. 当我看到这个错误,我得到这个SO链接,我不知道为什么同样的错误发生:

在VS 2008中无法编辑RDL报告(2005年认定中)

  1. 我可以看到下面还有的链接,该解决方案似乎是为他们工作的代码匹配:

http://www.dotnetspider.com/resources/28409-ReportViewer-with-multiple-dataset-report.aspx

在这一点上我已经用完了解决方案,并需要帮助。

编辑:

是什么造成了摆在首位的问题?

为了延长问题进行额外的数据集,我在RDL设计师抄袭表。 设计者似乎将改变<rd:TypeName>标记<TypeName>

在这一点上我是StackOverflow的真的很感谢为我提供这个平台发布自己的问题,并@RezaAghaei,谁在多个场合已经加倍努力来看待一个问题的所有细节给有需要的时候一个解决方案。

Answer 1:

作为例外处理,元素Field具有无效的子元素TypeName

您应该使用rd:TypeName代替。 例如:

<Field Name="id">
  <DataField>id</DataField>
  <rd:TypeName>System.Int32</rd:TypeName>
</Field>

问题是与第二个及以后的数据集,但第一个数据集是OK。



文章来源: C# Winform – Extending solution working for single DataSet to Multiple DataSets for ReportViewer Reports with the help of RDL file