Cass Cass - 1 month ago 8
C# Question

How do I get data from a dataset to show in a ReportViewer?

I have created a ReportViewer within a WindowsFormHost. Here is my XAML code.

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


I am calling a stored procedure in my database to get the data. I use the Report Wizard to create my report rdlc. I called it
PrintWeeklyList.rdlc
. For Choose a DataSource Type I selected Database. For Choose a Database Model I select DataSet. For Database Object I choose a stored procedure called
GetPrintWeeklyListData
. I gave the Data Source the name
PrintWeeklyListDataSet
. I gave the DataSet the name
PrintWeeklyListDS
.

Here is my C# code to load the the ReportViewer:

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;
}
}


When I step through the code I see that both dataset and rvWeeklyList have the data from the stored procedure in them. I have been searching Google and I can't figure out what I am missing to get the data to show. Any code examples would be greatly appreciated.

sly sly
Answer

First, change WindowsFormsHost to Visibility="Visible"

Second, ensure that the PrintWeeklyList.rdlc file's Build Action is set to Embedded Resource

Now use the revised code below and be sure to replace the substring<VisualStudioProjectName> with the name of your Visual Studio project for the ReportEmbeddedResource:

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 article reference: https://msdn.microsoft.com/en-us/library/hh273267.aspx