ReportViewer for WPF and Silverlight | ComponentOne
C1ReportViewer Task-Based Help / Loading Documents into C1ReportViewer / Loading Documents from Files on the Client Machine (Silverlight)
In This Topic
    Loading Documents from Files on the Client Machine (Silverlight)
    In This Topic

    In this example, you'll set up the application so that users can load a file from the local file system. You will add a button to the application and then add code to choose and open a file at run time. Because this example uses the OpenFileDialog control this code must be executed in a Button Click event. Note that this topic assumes you have added a C1ReportViewer control named "C1ReportViewer1" to your application.

    Complete the following steps.

    1. Open the MainPage.xaml file in your application, and open XAML view.
    2. Add the following markup to add a button control to the application.
      <Button Content="Load File" Height="23" Name="Button1" Width="70" Click="Button1_Click" />
      
    3. Right-click the page and select View Code. In Code View you'll add code to initialize the button you added in the previous step.
    4. Add the following imports statement at the top of the page:

       

      Visual Basic
      Copy Code
      Imports C1.Silverlight.ReportViewer
      

       

      C#
      Copy Code
      using C1.Silverlight.ReportViewer;
      

       

    5. Add the following Button_Click event handler code:

       

      Visual Basic
      Copy Code
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
          Dim dialog = New OpenFileDialog()
          dialog.Filter = "PDF files|*.pdf|HTML files|*.html;*.mhtml"
          If dialog.ShowDialog() = True Then
              Using fileStream = dialog.File.OpenRead()
                  Try
                      C1ReportViewer1.LoadDocument(fileStream)
                  Catch ex As Exception
                      MessageBox.Show("Failed to load document.")
                  End Try
              End Using
          End If
      End Sub
      

       

      C#
      Copy Code
      private void Button1_Click(System.Object sender, System.Windows.RoutedEventArgs e)
      {
         dynamic dialog = new OpenFileDialog();
         dialog.Filter = "PDF files|*.pdf|HTML files|*.html;*.mhtml";
         if (dialog.ShowDialog() == true) {
               using (fileStream == dialog.File.OpenRead()) {
                     try {
                           C1ReportViewer1.LoadDocument(fileStream);
                     } catch (Exception ex) {
                           MessageBox.Show("Failed to load document.");
                     }
               }
         }
      }
      

       

      This code initializes a dialog box to be opened when the button is clicked. In the dialog box users can select a file to open in the C1ReportViewer control. Notice how, in the code above, the LoadDocument method allows you to load PDF and HTML content.

    6. Run the application.
    7. In the running application, click the Load File button. Notice that a dialog box appears, allowing you to choose a PDF or HTML file of your choice.
    8. Locate and select a PDF file on your local machine to open and then click the Open button. The dialog box will close and the file you selected will be loaded into the C1ReportViewer control.