Reports for WinForms | ComponentOne
Working with C1Report / Developing Reports for Desktop Scenarios / Reports Loaded at Run Time
In This Topic
    Reports Loaded at Run Time
    In This Topic

    Loading reports at run time requires a report definition file and works like a viewer. The main advantage of this type of application is that if you modify the report format, there's no need to update the application. Simply send the new report definition file to the users and you are done.

    To create an application with reports loaded at run time, follow these steps:

    1. Use the C1ReportDesigner application to create all the reports you will need. (See Working with C1ReportDesigner for details on how to do this.)
    2. Add the following controls to the application:
      • C1Report component named c1r
      • C1PrintPreview component named ppv
      • ComboList control named cmbReport
      • StatusBar control named status
    3. Add the following Import statements to the top of the file:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Imports C1.C1Report        
      Imports System.IO
      

      To write code in C#

      C#
      Copy Code
      using C1.C1Report;        
      using System.IO;
      

      This allows you to reference the C1Report and System.IO classes and objects without having to specify the full namespaces.

    4. Add code to read the report definition file and build a list of all reports in it. This can be done as follows:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' get application path        
      Dim appPath As String        
      appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower()        
      Dim i As Integer = appPath.IndexOf("/bin")        
      If (i < 0) Then i = appPath.IndexOf("\bin")        
      If (i > 0) Then appPath = appPath.Remove(i, appPath.Length - i)       
           
      ' get names of reports in the report definition file         
      m_ReportDefinitionFile = appPath & "\Data\Nwind.xml"       
      Dim reports As String() = c1r.GetReportInfo(m_ReportDefinitionFile)
          
      ' populate combo box       
      cmbReport.Items.Clear()       
      Dim report As String        
      For Each report In reports        
        cmbReport.Items.Add(report)        
      Next
      
      

      To write code in C#

      C#
      Copy Code
      // get application path        
       string appPath;        
      appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower();        
       int i = appPath.IndexOf("/bin");        
      if ((i < 0) ) { i = appPath.IndexOf("\bin"); }        
      if ((i > 0) ) { appPath = appPath.Remove(i, appPath.Length - i); }
       
      // get names of reports in the report definition file       
      m_ReportDefinitionFile = appPath + "\Data\Nwind.xml";        
       string ( reports) = c1r.GetReportInfo(m_ReportDefinitionFile);
              
      // populate combo box        
      cmbReport.Items.Clear();        
       string report;        
      foreach report In reports        
        cmbReport.Items.Add(report);        
      }
      

      The code starts by getting the location of the file that contains the report definitions. This is done using static methods in the system-defined Path and Application classes. You may have to adjust the code to reflect the location and name of your report definition file.

      Then it uses the GetReportInfo method to retrieve an array containing the names of all reports in the report definition file (created in step 1), and populates the combo box that will allow users to select the report.

    5. Add code to render the report selected by the user. For example:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub cmbReport_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbReport.SelectedIndexChanged        
        Try        
          Cursor = Cursors.WaitCursor
      
          ' load report        
          status.Text = "Loading " & cmbReport.Text        
          c1r.Load(m_ReportDefinitionFile, cmbReport.Text)
      
          ' render into print preview control        
          status.Text = "Rendering " & cmbReport.Text        
          ppv.Document = c1r
      
          ' give focus to print preview control        
          ppv.StartPage = 0        
          ppv.Focus()        
      
          Finally        
            Cursor = Cursors.Default        
          End Try        
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void cmbReport_SelectedIndexChanged(object sender, System.EventArgs e)        
      {        
        try {        
          Cursor = Cursors.WaitCursor;
       
          // load report        
          status.Text = "Loading " + cmbReport.Text;        
          c1r.Load(m_ReportDefinitionFile, cmbReport.Text);
       
          // render into print preview control        
          status.Text = "Rendering " + cmbReport.Text;        
          ppv.Document = c1r;
      
          // give focus to print preview control        
          ppv.StartPage = 0;        
          ppv.Focus();
         
          } finally {        
            Cursor = Cursors.Default;        
          }        
      }