繁体   English   中英

显示数据从datagridview到reportviewer C#

[英]Show data from datagridview to reportviewer C#

我有一个datagridview,我想将数据传递给reportviewer,因此我可以轻松地将其打印并导出为pdf / excel。 我该怎么办? 还是有其他解决方案来实现我的目标? 谢谢! :)

由于您要将GridView数据传递给ReportViewer,因此首先要做的是检索gridview的数据源,如下所示:

BindingSource bs = (BindingSource)GridView1.DataSource;//You should first convert DataSourse into Binding Sourse
DataTable dt = (DataTable) bs.DataSource; //Get GridView data source to Data table

现在,您将GridView数据保存在DataTable dt中 ,可以将ReportViewer绑定到DataTable,如下所示:

ReportViewer ReportViewer1 = new ReportViewer(); //Your ReportViewer Control
ReportDataSource rds = new ReportDataSource("DataSet1_Customers_DataTable1",dt); // ReportViewerDataSource : ReportViewer is to be bind to this DataSource
ReportViewer1.LocalReport.DataSources.Clear(); // Clear the Previous DataSource of ReportViewer
ReportViewer1.LocalReport.DataSources.Add(rds); //bind ReportViewer1 to the new datasource(Which you wish)
ReportViewer1.LocalReport.Refresh(); // Refresh the ReportViewer Control, ReportViewer1 in this case

就是这样,您完成了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM