从datagridview显示数据到报表查看器C# [英] Show data from datagridview to reportviewer C#

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

问题描述

我有一个datagridview,我想把数据传递给reportviewer,所以我可以很方便地打印和导出到pdf / excel。
请问我该怎么办?还是有另一种解决方案来实现我的目标?谢谢! :)

解决方案

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

  BindingSource bs =(BindingSource)GridView1.DataSource; //您应该首先将DataSourse转换为Binding Sourse 
DataTable dt =(DataTable)bs.DataSource; //获取GridView数据源到数据表

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

  ReportViewer ReportViewer1 = new ReportViewer(); // Your ReportViewer Control 
ReportDataSource rds = new ReportDataSource(DataSet1_Customers_DataTable1,dt); // ReportViewerDataSource:ReportViewer要绑定到这个DataSource
ReportViewer1.LocalReport.DataSources.Clear(); //清除ReportViewer的以前的数据源
ReportViewer1.LocalReport.DataSources.Add(rds); //将ReportViewer1绑定到新的数据源(你想要的)
ReportViewer1.LocalReport.Refresh(); //刷新ReportViewer控件,在这种情况下,ReportViewer1

这些都完成了! p>

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! :)

解决方案

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

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

thats all, you're done!

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

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