【问题标题】:RDLC Reporting using C#使用 C# 的 RDLC 报告
【发布时间】:2015-11-05 14:12:29
【问题描述】:

我对 Visual Studio C# 中的报告完全陌生..我尝试为初学者搜索一些教程,但我没有成功..我只是找到了并没有真正解释基础知识的代码示例...我写了一些代码符合并运行良好,但在 Visual Studio 2013 的报表查看器控件中没有显示任何内容。我的代码如下:

// This method is in a class named Customers.
// When the user clicks the first column of the datagrid view(I have placed a button 
// in the first column of the datagrid) a new form opens (ReportForm) and i pass
// the DataSet called dsReport to its constructor.


 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex == 0 )
            {

                DataSet dsReport = new DataSet();
                DataTable tbl = dsReport.Tables.Add();

                tbl.Columns.Add("CustomerName", typeof(string));
                tbl.Columns.Add("CustomerAddress", typeof(string));
                tbl.Columns.Add("MaritalStatus", typeof(string));
                tbl.Columns.Add("CustomerType", typeof(string));
                tbl.Columns.Add("ImagePath", typeof(string));

                foreach (Customer cust in customerList)
                {
                    DataRow dr = dsReport.Tables[0].NewRow();
                    dr["CustomerName"] = cust.Name;
                    dr["CustomerAddress"] = cust.Address;
                    dr["MaritalStatus"] = cust.MaritalStatus;
                    dr["CustomerType"] = cust.CustomerType;
                    dr["ImagePath"] = cust.ImagePath;

                    dsReport.Tables[0].Rows.Add(dr);
                }

                ReportForm report = new ReportForm(dsReport);
                report.Show();

            }
        }


//Following is the code for the ReportForm Class
//I do not get any results in the report viewer
//I just see the message "The source of the report definition has not  been specified"

public ReportForm(DataSet dsReport)
        {
            InitializeComponent();

            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(myReportSource);
            this.reportViewer1.ProcessingMode = ProcessingMode.Local;
            this.reportViewer1.LocalReport.Refresh();
            this.reportViewer1.RefreshReport();

        }

        private void ReportForm_Load(object sender, EventArgs e)
        {

            this.reportViewer1.RefreshReport();

        }

/* 请注意,我已经在调试器中运行了代码,并且数据集正在 正确填充,reportViewer1.LocalReport..我也没有 向项目添加了任何数据源,但我没有添加任何报告文件(.rdl)文件 到项目 */

最后请大家回答以下问题:

第一季度。我绝对必须包含一个数据源来处理报告吗 查看工具??

第二季度。我是否必须在项目中包含 .rdl 文件才能显示报告?

第三季度。报表查看器工具和 .rdl 文件是否相同? 不一样??

【问题讨论】:

    标签: c# reporting rdlc


    【解决方案1】:

    ReportViewer 是一个知道如何呈现报表的控件。它只是处理绘图和其他一些后台任务,它不是实际的报告。实际报告是.rdl 文件(报告定义语言)。它包含生成报告的所有说明,但不包含实际数据。 DataSource 包含报表操作的数据。

    所以专门回答你的问题:

    1. 是的(除非您的报告是完全静态的并且不使用任何数据)。

    2. 不,但您需要以某种方式将.rdl 发送到 ReportViewer。如果您不想将其作为文件包含在内,则可以将其作为资源嵌入到应用程序中,甚至可以将其硬编码为字符串。 ReportViewer 有一个接受Stream 的方法,因此任何可以提供流的东西都可以作为.rdl 的源。

    3. 正如我一开始解释的那样,它们是不同的。

    【讨论】:

    • 感谢 Bradley Uffner 的简短、清晰和简洁的回答...请考虑以下场景...我已将数据源和 .rdl 文件添加到项目中并成功绑定到报表查看器...报表查看器现在可以正常工作并显示所有记录...假设我有一个名为 Form1 的表单,其中包含一个具有 5 列的 DatagridView ..现在当单击任何行的第一列时,一个新的表单与报表查看器一起出现,并且仅在报表查看器中显示该行...我该如何解决这个问题???
    • 在按钮上单击将数据行复制到新的 DataTable,并将该 DataTable 用作报表的数据源。该报告只能对您传递给它的数据起作用。
    • Bradley Uffner 我在项目中添加了一个数据集以及一个 .rldc 文件。然后我在表单中添加了一个reportviewer,然后将数据集和.rldc 文件与报表绑定,它就可以工作了。我使用参数来获取单行数据......现在这种方法的问题是数据集和 .rldc 绑定到报表查看器。由于自动应用的绑定,我无法在运行时通过代码更改数据集...您能告诉我如何通过代码手动完成所有这些工作...如果可能,请显示工作代码..另外请从一开始就查看我的代码。
    • 我相信您需要在更改数据或过滤器后致电.RefreshReportmsdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    相关资源
    最近更新 更多