FlexReport for WinForms | ComponentOne
In This Topic
    Data Binding
    In This Topic

    You learnt how to create a report definition and save it in .flxr files using the FlexReportDesigner application. In this topic, you will see how to load the data from a data source or execute data-binding using the FlexReport component, which loads and displays data from the data source specified in the report definition file.

    For successful data binding, FlexReport needs the actual data to create the report. In most cases, the data is fetched from a database, but there are other options as well. The following sections explore how to retrieve data from different sources.

    Retrieve Data from Database

    To retrieve or load the report data in FlexReport, set the following DataSource properties in C1FlexReport class:

    Properties Description
    ConnectionString The ConnectionString property specifies the database that contains the data.
    RecordSource The RecordSource property specifies which table, stored procedure, or SQL command to use for retrieving the data.

    The code snippet depicts how to set the data source programmatically using these properties:

    C#
    Copy Code
    // initialize DataSource 
    DataSource ds = flexReport.DataSource;
    ds.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\GPCTAdmin\Documents\ComponentOne Samples\Common\C1NWind.mdb";
    ds.RecordSource = "Employees";
    

    Once these properties are set, C1FlexReport initializes the data source and uses them to load the data from the database automatically. This is same as initializing data source through the code or the designer as illustrated in Quickstart.

    Retrieve Data from Stored Procedure

    Stored procedures (or sprocs) can assist you to achieve a systematic implementation of logic across applications, improve performance, and shield users from needing to know the details of the tables in the database. One of the major advantages of stored procedures is that you can pass in parameters to have the database filter the recordset. This returns a smaller set of data, which is quicker and easier for the report to manipulate.

    You can populate a report from a stored procedure in the FlexReport Wizard. To open the FlexReport Wizard complete one of the following:

    Populating a report from a stored procedure is not different from using SQL statements or straight tables. In the 'Step 1: Select the Data Source for the new report.' window of the FlexReport Wizard, click the ellipsis button to choose a datasource. Then, choose a Stored Procedure from the list of available Data sources:

    Select the Data Source for the new report.

    Select Next to continue through the wizard.

    When it comes to loading other forms of data, you have two options:

    Retrieve Data from DataTable Objects

    Many applications need to work on data outside FlexReport and load it into DataTable objects. In such cases, you can use DataTable objects as report data sources, avoiding the need to load them again while rendering the report.

    This approach is also useful in cases where:

    To use a DataTable object as a FlexReport data source, simply load the report definition and then assign the DataTable to Recordset property of DataSource class. For example:

    C#
    Copy Code
    // load DataTable from cache or from a secure/custom provider
     DataTable dt = GetMyDataTable();
     // load report definition (before setting the data source)
     c1FlexReport1.Load(@"reportFile", "reportName");
     // use DataTable as the data source
     c1FlexReport1.DataSource.Recordset = dt;
    

    Retrieve Data from Custom DataSource Objects

    You can use custom objects as data sources. The only requirement is that the custom object must implement the IC1FlexReportRecordset interface.

    IC1FlexReportRecordset is a simple interface that can be added to virtually any collection of data with ease. In most scenarios, this is more efficient than creating a DataTable object and copying all the data into it. For example, you could use custom data source objects to wrap a file system or custom .xml or .flxr files.

    To use custom data source objects, load the report definition and then assign the object to the C1FlexReport's Recordset property. For example:

    C#
    Copy Code
    // get custom data source object
    IC1FlexReportRecordsetrs=(IC1FlexReportRecordset)GetMyCustomDataSource();
    // load report definition before setting the datasource
    flexreport.Load(@"reportFile","reportName");
     
    //use custom datasource object in FlexReport component
    flexreport.DataSource.Recordset=rs;
    
    Note: WinForms .NET 6 Edition does not include rich design-time support yet. We will enhance it in the future releases.