我在WindowsFormHost中创建了一个ReportViewer。这是我的XAML代码。
<Grid x:Name="grdPrintingGrid" Visibility="Visible" Height="440" Margin="105, 0, 0 0" VerticalAlignment="Top" Grid.ColumnSpan="2" >
<TextBox x:Name="txtPRTTitle" IsReadOnly="True" HorizontalAlignment="Left" Height="32" Margin="10,4,0,0" VerticalAlignment="Top" Width="450" FontFamily="Arial" FontSize="18.667" Background="#FFE8F9FF" BorderThickness="0"/>
<WindowsFormsHost x:Name="wfhFormsHost" Visibility="Hidden" HorizontalAlignment="Left" Height="312" Margin="10,54,0,0" VerticalAlignment="Top" Width="683">
<rv:ReportViewer x:Name="rvWeeklyList" />
</WindowsFormsHost>
<Grid x:Name="grdPWLGrid" Visibility="Visible" Height="440" Margin="0, 0, 0 0" VerticalAlignment="Top">
<DataGrid IsReadOnly="True" Name="dgPWLCGrid" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Background="#FFE8F9FF" HorizontalAlignment="Left" VerticalAlignment="Top" Height="255" Margin="10,62,0,0" Width="693" BorderThickness="1" BorderBrush="Black" CanUserResizeRows="False" >
</DataGrid>
<Button x:Name="btnPWLPrint" Content="Print" HorizontalAlignment="Left" Height="26" Margin="307,388,0,0" VerticalAlignment="Top" Width="74" FontFamily="Arial" FontSize="14.667" Background="#FFEEFFFF" Click="btnPWLPrint_Click"/>
</Grid>
</Grid>
我正在调用数据库中的存储过程来获取数据。我使用报表向导创建我的报表rdlc。我叫它PrintWeeklyList.rdlc
。对于选择数据源类型,我选择了数据库。对于选择数据库模型,我选择DataSet。对于数据库对象,我选择一个名为GetPrintWeeklyListData
的存储过程。我给数据源命名为PrintWeeklyListDataSet
。我将数据集命名为PrintWeeklyListDS
。
下面是我加载ReportViewer的c#代码:
private void RVWeeklyList_Load(object sender, EventArgs e)
{
if (!isReportViewerLoaded)
{
ReportViewer rvWeeklyList = new ReportViewer();
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 =
new Microsoft.Reporting.WinForms.ReportDataSource();
PrintWeeklyListDataSet dataset = new PrintWeeklyListDataSet();
dataset.BeginInit();
reportDataSource1.Name = "PrintWeeklyListDS"; //Name of the report dataset in our .RDLC file
reportDataSource1.Value = dataset.GetPrintWeeklyListData;
rvWeeklyList.LocalReport.DataSources.Add(reportDataSource1);
//rvWeeklyList.ServerReport.GetDataSources();
rvWeeklyList.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
rvWeeklyList.LocalReport.ReportEmbeddedResource = "PrintWeeklyList.rdlc";
dataset.EndInit();
PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter pwlTableAdapter =
new PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter();
pwlTableAdapter.ClearBeforeFill = true;
pwlTableAdapter.Fill(dataset.GetPrintWeeklyListData);
rvWeeklyList.LocalReport.Refresh();
rvWeeklyList.RefreshReport();
isReportViewerLoaded = true;
}
}
当我逐步执行代码时,我看到dataset和rvWeeklyList中都有来自存储过程的数据。我一直在搜索谷歌,我不知道我错过了什么,以获得数据显示。如有任何代码示例,将不胜感激。
首先,将WindowsFormsHost
更改为Visibility="Visible"
PrintWeeklyList.rdlc
文件的Build Action
被设置为Embedded Resource
现在使用下面修改后的代码,并确保将子字符串<VisualStudioProjectName>
替换为ReportEmbeddedResource
的Visual Studio项目名称:
public MainWindow()
{
InitializeComponent();
rvWeeklyList.Load += RVWeeklyList_Load;
}
private void RVWeeklyList_Load(object sender, EventArgs e)
{
if (!isReportViewerLoaded)
{
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 =
new Microsoft.Reporting.WinForms.ReportDataSource();
PrintWeeklyListDataSet dataset = new PrintWeeklyListDataSet();
dataset.BeginInit();
reportDataSource1.Name = "PrintWeeklyListDS";
reportDataSource1.Value = dataset.GetPrintWeeklyListData;
rvWeeklyList.LocalReport.DataSources.Add(reportDataSource1);
rvWeeklyList.LocalReport.ReportEmbeddedResource =
@"<VisualStudioProjectName>.PrintWeeklyList.rdlc";
dataset.EndInit();
PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter pwlTableAdapter =
new PrintWeeklyListDataSetTableAdapters.GetPrintWeeklyListDataTableAdapter();
pwlTableAdapter.ClearBeforeFill = true;
pwlTableAdapter.Fill(dataset.GetPrintWeeklyListData);
rvWeeklyList.RefreshReport();
isReportViewerLoaded = true;
}
}
MSDN文章参考:https://msdn.microsoft.com/en-us/library/hh273267.aspx