如何将数据表加载为 ReportDataSource?

发布于 2024-09-28 21:22:59 字数 595 浏览 2 评论 0原文

我想做类似的事情:

this.reportViewer.LocalReport.DataSources.Clear();
DataTable dt = new DataTable();
dt = this.inputValuesTableAdapter.GetData();    
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource();

rprtDTSource = dt; // this line generates exception   

//this.reportViewer.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer.RefreshReport();

如何将数据表加载为 ReportDataSource?

当前代码产生: “无法将类型‘System.Data.DataTable’隐式转换为‘Microsoft.Reporting.WinForms.ReportDataSource’”

I am trying to do something like:

this.reportViewer.LocalReport.DataSources.Clear();
DataTable dt = new DataTable();
dt = this.inputValuesTableAdapter.GetData();    
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource();

rprtDTSource = dt; // this line generates exception   

//this.reportViewer.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer.RefreshReport();

How can I load datatable as ReportDataSource?

The current code produces:
"Cannot implicitly convert type 'System.Data.DataTable' to 'Microsoft.Reporting.WinForms.ReportDataSource' "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤 2024-10-05 21:22:59

您没有正确初始化 ReportDataSouce。尝试一下:

this.reportViewer.LocalReport.DataSources.Clear(); 
DataTable dt = new DataTable(); 
dt = this.inputValuesTableAdapter.GetData();     

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt); 

this.reportViewer.LocalReport.DataSources.Add(rprtDTSource); 
this.reportViewer.RefreshReport(); 

此外,您可能需要更改 ReportDataSource 构造函数的第一个参数,以设置报表所需的数据源的名称。

You are not initializing the ReportDataSouce correctly. Give this a try:

this.reportViewer.LocalReport.DataSources.Clear(); 
DataTable dt = new DataTable(); 
dt = this.inputValuesTableAdapter.GetData();     

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt); 

this.reportViewer.LocalReport.DataSources.Add(rprtDTSource); 
this.reportViewer.RefreshReport(); 

Also, you might need to alter the first parameter to the ReportDataSource constructor to set the name of the datasource that your report is expecting.

兔姬 2024-10-05 21:22:59

我相信上面 madisonw 的答案是正确的,但 Luke 关于使用 .rdlc 报告文件中指定的数据集名称作为 Datasource.Name 属性的评论可能需要强调。对我来说,这是导致我的应用程序无法运行的主要问题。可以通过使用“打开方式...”命令将 .rdlc 文件作为 XML 文件打开来找到它。我认为默认值只是“DataSet1”:

<DataSets>
  <DataSet Name="DataSet1">
    <Fields>
      <Field Name="BusinessEntityID">
        <DataField>BusinessEntityID</DataField>
        <rd:TypeName>System.Int32</rd:TypeName>
      </Field>

我犯的另一个错误是没有将 .rdlc 文件包含在调试(或发布)文件夹中。通过右键单击“解决方案资源管理器”中的 .rdlc 文件,然后单击“属性”,然后将“复制到输出目录”设置为“始终复制”,可以修复此问题。

一旦这两部分得到纠正,我的程序就可以在控制台应用程序中使用 ReportViewer 来生成没有界面的 PDF 文件。

I believe that madisonw's answer above is correct, but Luke's comment about using the DataSet name as named in the .rdlc report file, for the Datasource.Name property, perhaps needs to be emphasized. For me this was the main bugaboo that kept my app from working. It can be found by opening the .rdlc file as an XML file by using the "Open with..." command. The default I think is simply "DataSet1":

<DataSets>
  <DataSet Name="DataSet1">
    <Fields>
      <Field Name="BusinessEntityID">
        <DataField>BusinessEntityID</DataField>
        <rd:TypeName>System.Int32</rd:TypeName>
      </Field>

One other mistake I made was not including the .rdlc file in the Debug (or Release) folder. This was fixed by right-clicking on the .rdlc file in Solution Explorer, then Properties, then setting "Copy to Output Directory" to "Copy Always".

Once these two parts were corrected my program worked to use ReportViewer within a console app to generate a PDF file with no interface.

魄砕の薆 2024-10-05 21:22:59
this.reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.Reset();
reportViewer1.LocalReport.ReportEmbeddedResource = "Your Report Name.rdlc";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Connection";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from YourTableName";
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();     
ReportDataSource rprtDTSource= new ReportDataSource();
rprtDTSource.Name = "reportDataSetName";
rprtDTSource.Value = dt;
this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer1.RefreshReport();
this.reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.Reset();
reportViewer1.LocalReport.ReportEmbeddedResource = "Your Report Name.rdlc";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Connection";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from YourTableName";
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
con.Close();     
ReportDataSource rprtDTSource= new ReportDataSource();
rprtDTSource.Name = "reportDataSetName";
rprtDTSource.Value = dt;
this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer1.RefreshReport();
梦毁影碎の 2024-10-05 21:22:59

文件的 DataSources 块如下所示:

想象一下RDLC 数据集>
<数据集名称=“DataSet1_Lot”>

那么相关代码应该是:

string name = "DataSet1_Lot"; // 这必须存在于 RDLC 文件中
DataTable dt = new DataTable();

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);

Imagine the DataSources block of your RDLC file is like below:

< DataSets >
< DataSet Name="DataSet1_Lot" >

then the relative code should be:

string name = "DataSet1_Lot"; // this must exist in the RDLC file
DataTable dt = new DataTable();

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文