簡體   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