将数据从 datagridview 显示到 reportviewer C# [英] Show data from datagridview to reportviewer C#

查看:37
本文介绍了将数据从 datagridview 显示到 reportviewer C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 datagridview,我想将数据传递给 reportviewer,这样我就可以轻松地打印和导出到 pdf/excel.请问我该怎么做?还是有另一种解决方案来实现我的目标?谢谢!:)

I have a datagridview and I would like to pass data to reportviewer, so in this way I can print and export to pdf/excel easily. How can I do it please? or is there another solution to get my goal? Thank you! :)

推荐答案

既然要将 GridView 数据传递给 ReportViewer,那么首先要做的就是检索 gridview 的数据源,如下图:

Since you want to pass GridView data to ReportViewer, the first thing you got to do is retrieve the datasource of gridview as shown below:

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

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

Now You got your GridView data in DataTable dt, you can bind ReportViewer to DataTable as shown below:

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

就是这样,你完成了!

这篇关于将数据从 datagridview 显示到 reportviewer C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆