1.选择“工具”>“Nuget包管理器”>“程序包管理器控制台”
2.执行命令:Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms -Pre
3.在项目-引用上点右键,选择“添加引用”,在.net framework组建选项卡中选择“浏览”,然后在你的工程根目录下的“\packages\Microsoft.ReportingServices.ReportViewerControl.Winforms.140.1000.523\lib\net40”目录下选择“Microsoft.ReportViewer.WinForms.dll”等文件,然后report viewer就被添加到你的项目里了!
WPF做Reportviewer的实例
一、
添加引用后,在wpf项目xmal中引入命名空间
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
添加一个reportviewer
<WindowsFormsHost>
<rv:ReportViewer x:Name="_reportViewer" Width="300"/> <!--ProcessingMode="Remote"远程处理方式-->
</WindowsFormsHost>
二、
右键点击项目 添加-新建项
点击添加按钮,选择自己的数据源数据集
点击下一步按钮,排列字段(具体自己尝试)
点击下一步选择布局,下一步选择样式(无关紧要,不再截图),点击完成
三、
在xaml.cs后台代码文件中添加如下代码
/// <summary>
/// DetectorQuery.xaml 的交互逻辑
/// </summary>
public partial class DetectorReportAndQuery : UserControl
{
private bool _isReportViewerLoaded;
public DetectorReportAndQuery()
{
InitializeComponent();
_reportViewer.Load += _reportViewer_Load;
}
private void _reportViewer_Load(object sender, EventArgs e)
{
if (!_isReportViewerLoaded)
{
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new
Microsoft.Reporting.WinForms.ReportDataSource();
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource2 = new
Microsoft.Reporting.WinForms.ReportDataSource(); //第二个数据源,可在下方的红色字体中找到创建方法
InternalRadiationSystemDataSet dataset = new InternalRadiationSystemDataSet();
dataset.BeginInit();
reportDataSource1.Name = "DataSet";
reportDataSource1.Value = dt;
reportDataSource2.Name = "DataSet1";
reportDataSource2.Value = listMeasureData;
this._reportViewer.LocalReport.DataSources.Add(reportDataSource1);
this._reportViewer.LocalReport.DataSources.Add(reportDataSource2);
this._reportViewer.LocalReport.ReportPath = "../../Report/ReportDetail.rdlc";
dataset.EndInit();
InternalRadiationSystemDataSetTableAdapters.ReportViewTableAdapter
ReportInfo = new
InternalRadiationSystemDataSetTableAdapters.ReportViewTableAdapter();
ReportInfo.ClearBeforeFill = true;
ReportInfo.Fill(dataset.ReportView);
if (dataset.ReportView.Rows.Count == 0)
{
MessageBox.Show("没有查询到相应记录!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
if (dataset.ReportView.Select(filter).FirstOrDefault()!=null)
{
dt.Rows.Add(dataset.ReportView.Select(filter).FirstOrDefault().ItemArray);
}
//Padding margin = new Padding();
//_reportViewer.Margin = "20,0";
_reportViewer.RefreshReport();
_isReportViewerLoaded = true;
}
}
}
F5运行即可看到效果
ps:
1.InternalRadiationSystemDataSet 指的是第二张图中你创建报表向导时选择的数据源
2.新建的当然只有一个数据源,但在设计中可以再添加数据源和数据集
如有不足欢迎指正 谢谢