C# 数据表作为 ReportViewer 中的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11725634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatable as datasource in ReportViewer
提问by seeker
I want the table component in reportviewer control to be filled in with data from datatable. In other words, i want to use datatable as source for reportviewer control. I tried to create dataset, added datatable with exact columns that my datatable will have after programmatical fill in. Then I used the following code:
我希望使用数据表中的数据填充报表查看器控件中的表格组件。换句话说,我想使用数据表作为报表查看器控件的源。我尝试创建数据集,添加了数据表,其中包含我的数据表在以编程方式填充后将具有的确切列。然后我使用了以下代码:
DataTable dt = new DataTable();
dt.TableName = "DataTable1";
conn.Open();
adapter.Fill(dt);
ReportViewer1.ProcessingMode=ProcessingMode.Local;
ReportDataSource source = new ReportDataSource("SampleDs", dt);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(source);
ReportViewer1.DataBind();
ReportViewer1.LocalReport.Refresh();
However, that does not work. The only message I get is:
但是,这是行不通的。我得到的唯一消息是:
An error has occurred during report processing. SampleDs.
报告处理期间发生错误。样本 D。
Can anyone tell me how to solve issue or point out to the refference where full process of creating such report described,
谁能告诉我如何解决问题或指出参考文献,其中描述了创建此类报告的完整过程,
采纳答案by Jay Riggs
The overload you're using for the constructor of the ReportDataSourceobject is expecting the name of the data source in that first parameter. You're not supplying this, you need the DataTable name.
您用于ReportDataSource对象构造函数的重载需要第一个参数中的数据源名称。你没有提供这个,你需要数据表名称。
Update your code to this and you should be OK:
将您的代码更新为此,您应该没问题:
ReportDataSource source = new ReportDataSource("DataTable1", dt);

