c# - 将数据从 datagridview 显示到 reportviewer C#

标签 c# excel pdf datagridview reportviewer

我有一个 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

现在您在 DataTable dt 中获得了您的 GridView 数据,您可以将 ReportViewer 绑定(bind)到 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

就这样,你完成了!

关于c# - 将数据从 datagridview 显示到 reportviewer C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314032/

相关文章:

c# - VisualStateManager.GoToState 不适用于 DataTemplate

vba - 如何使用 Excel VBA 2010 创建具有两个数据集的图表

excel - 当单元格打开并选择另一个工作表时,Workbook_sheetChange 崩溃

python - HTML 到 PDF 尺寸不正确

c# - 在代码中创建绑定(bind) (WinRT)

c# - 如何使用 ImmutableList<> 而不是 List<> 来获得线程安全?

c# - 如何从列表列表中选择项目

vba - Excel 单元格默认度量单位

html - 如何在没有mysql数据库中的html标签的情况下打印pdf?

pdf - 如何在 PDF 中设置填充 Alpha