2015-11-05 173 views
0

我是完全新的报告Visual Studio C#..我尝试寻找初学者的一些教程,但我不成功..我只是发现代码示例,没有真正解释的基础知识...我写了要求,并且运行良好,但如下它不会显示在Visual Studio 2013..My代码报表查看器控制任何事情都是一些代码:RDLC报告使用C#

// 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..Also我没有 添加任何数据源到项目,我没有添加任何报告文件(.rdl)文件 到项目*/

最后谁能请回答下列问题:

Q1。我是否必须包含一个数据源才能使用报告 查看器工具? Q2302。我是否必须在项目中包含.rdl文件才能显示报告?

Q3。报告查看器工具和.rdl文件是否相同或是否与 不同?

回答

2

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

所以具体回答你的问题:

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

  2. 不,但您需要以某种方式获得ReportViewer的.rdl。如果你不想把它作为一个文件包含,你可以将它作为一个资源嵌入到你的应用程序中,甚至可以将它作为一个字符串硬编码。 ReportViewer的方法也接受Stream,所以任何可以提供流的东西都可以作为.rdl的源。

  3. 它们是不同的,正如我在最初解释的那样。

+0

谢谢布拉德利Uffner的简短,清晰和简洁的答案...请考虑以下情况......我已经将数据源和.rdl文件添加到项目并已成功绑定到该报告查看器...报告查看器现在正常工作并显示所有记录...假设我有一个名为Form1的窗体,其中包含一个带有5列的DatagridView ..现在,当单击任何一行的第一列时出现一个新窗体使用报告查看器并仅在报告查看器中显示该行...我该如何解决此问题? – user2423083

+0

在按钮上单击将数据行复制到新的DataTable,然后使用该DataTable作为报告的数据源。报告只能处理您传递的数据。 –

+0

布拉德利乌弗纳我添加了一个数据集的项目和一个.rldc文件。然后,我将一个reportviewer添加到表单中,然后用报告AND IT WORKED绑定数据集和.rldc文件。我使用参数来获取单行数据...现在这种方法的问题是数据集和.rldc被绑定到报表查看器。由于自动应用的绑定,我无法在运行时通过代码更改数据集...你能告诉我如何通过代码手动完成所有这些工作......如果可能的话,请显示工作代码..也请在开始时看到我的代码。 – user2423083