ComponentOne FlexReport for WinForms
Load FlexReport at Run Time
Working with FlexReport > Developing FlexReport for Desktop > Load FlexReport at Run Time

Loading reports at run time requires a report definition file and 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. Create all the required reports in the C1FlexReportDesigner application. For more information, see Working with C1FlexReportDesigner.
  2. Add the following controls to the application:
    • C1FlexReport component named c1FlexReport1
    • C1FlexViewer control named fv
    • ComboBox control named cmbReport
    • Button control named button1
  3. Add the following Import statements to the top of the file:
    Imports C1.Win.FlexReport       
    Imports System.IO
    
    using C1.Win.FlexReport;
    using System.IO;
    

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

  4. Add the following code in the button click event to read the report definition file and build a list of all reports:
           
    ' get application path       
    Dim appPath As String
    appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower()
    Dim i As Integer = appPath.IndexOf(vbBack & "in")
    If (i < 0) Then
        i = appPath.IndexOf(vbBack & "in")
    End If
    If (i > 0) Then
        appPath = appPath.Remove(i, appPath.Length - i)
    End If
    ' get names of reports in the report definition file      
    m_ReportDefinitionFile = appPath & Convert.ToString("\Data\Products Report.flxr")
    Dim reports As String() = C1FlexReport.GetReportList(m_ReportDefinitionFile)
    ' populate combo box       
    cmbReport.Items.Clear()
    
    For Each report As String In reports
        cmbReport.Items.Add(report)
    Next
    
    // 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\Products Report.flxr";
    string[] reports = C1FlexReport.GetReportList(m_ReportDefinitionFile);
    // populate combo box       
    cmbReport.Items.Clear();
    
    foreach (string 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 GetReportList 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 allowing users to select the report.

  5. Add code to render the report selected by the user. For example:
    Try
        Cursor = Cursors.WaitCursor
    
        ' load report        
        fv.StatusText = "Loading" + cmbReport.Text
        C1FlexReport1.Load(m_ReportDefinitionFile, cmbReport.Text)
    
    
        ' render into print preview control        
        fv.StatusText = "Rendering" + cmbReport.Text
        fv.DocumentSource = C1FlexReport1
    
        ' give focus to print preview control 
        fv.Focus()
    Finally
        Cursor = Cursors.[Default]
    End Try
    
    try
    {
        Cursor = Cursors.WaitCursor;
    
        // load report        
        fv.StatusText = "Loading" + cmbReport.Text;
        c1FlexReport1.Load(m_ReportDefinitionFile, cmbReport.Text);
    
    
        // render into print preview control        
        fv.StatusText = "Rendering" + cmbReport.Text;
        fv.DocumentSource = c1FlexReport1;
    
        // give focus to print preview control 
        fv.Focus();
    }
    finally
    {
        Cursor = Cursors.Default;
    }
    
  6. Run the project.