Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将datatable加载为ReportDataSource?_C#_Reporting_Reportviewer - Fatal编程技术网

C# 如何将datatable加载为ReportDataSource?

C# 如何将datatable加载为ReportDataSource?,c#,reporting,reportviewer,C#,Reporting,Reportviewer,我正在尝试做一些类似的事情: this.reportViewer.LocalReport.DataSources.Clear(); DataTable dt = new DataTable(); dt = this.inputValuesTableAdapter.GetData(); Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDat

我正在尝试做一些类似的事情:

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();
如何将datatable加载为ReportDataSource

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

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

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构造函数的第一个参数,以设置报表所需的数据源的名称。

想象一下RDLC文件的DataSources块如下所示:

<数据集>

那么相关代码应该是:

string name=“DataSet1\u Lot”;//这必须存在于RDLC文件中 DataTable dt=新的DataTable()


Microsoft.Reporting.WinForms.ReportDataSource rprtDataSource=新的Microsoft.Reporting.WinForms.ReportDataSource(名称,dt)

我相信上面madisonw的回答是正确的,但是Luke关于使用.rdlc报告文件中命名的DataSet名称作为Datasource.name属性的评论可能需要强调。对我来说,这是阻止我的应用程序工作的主要原因。可以通过使用“openwith…”命令将.rdlc文件作为XML文件打开来找到它。我认为默认值只是“DataSet1”:


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


纠正这两个部分后,我的程序在控制台应用程序中使用ReportViewer生成一个没有接口的PDF文件。

“尚未为数据源提供数据源实例”…:(正如我在第一篇文章末尾提到的,您需要将ReportDataSource构造函数的第一个参数设置为报表正在查找的数据源的名称。该名称可能出现在您现在收到的错误消息中。因此,请使用包含该名称的字符串替换dt.TableName。我使用了与上面相同的代码,但适用于ion没有显示任何内容。它将处于挂起状态。请帮助我解决此问题。除了提供代码外,答案还应说明代码的作用以及如何解决此问题。
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();
<DataSets>
  <DataSet Name="DataSet1">
    <Fields>
      <Field Name="BusinessEntityID">
        <DataField>BusinessEntityID</DataField>
        <rd:TypeName>System.Int32</rd:TypeName>
      </Field>