Skip to main content Skip to footer

Binding C1Report to Collection DataSource

ComponentOne Reports for WPF extends the flexibility of C1Reports for Winforms and help its users to integrate reporting and document generating functionality into your Windows Presentation Foundation (WPF) application. With loads of functionality, C1Report for WPF provides the basic concept of DataBinding, generating SubReports etc. For more overview of WPF C1Report, you can visit the given documentation link. In this blog, I am going to present small and easy utility blog to bind a C1Report to a ObservableCollection datasource.

Create Report Definition File

To begin with we need a Report Definintion File (XML) which will be Loaded in the Report object. You can create a new Report Definition File using C1ReportDesigner application. For this blog implementation, we have to generate a report which is unbound. The easiest way to start a new report is to use the C1Report Wizard. In the C1Report Wizard click the Application button and select New from the menu to create a new report. To access the C1Report Wizard, click the New Report button from the Reports tab. Since we have to create a unbound Report, click Next on the DataSource screen and follow the same process to skip all the settings by clicking Next On each screen till Step 4. In the last screen (Step 5), provide a Name for the report "_C1Report_ListDataSource_" and select the option 'Modify the Report's Design'. Click Finish. We will start by defining the Field headers. Drag 6 Label Controls from the top Ribbon Bar into the Page Header section. Position the Label controls to avoid overlapping and then set their Text properties to following text.

  1. Title
  2. Name
  3. Basic
  4. Salary
  5. Department
  6. Subject

The above names are similar to the Field Names which we will define later in the DataSource. So once we are done with Captions, we will move to the Details Section where we will place the Labels controls which will be actually bound to the datasource fields. Once again drag the 6 Labels controls and set their individual Text Property to the text listed above. Important thing to be done is to set the Calculated property for these Label controls to True. Calculated property specifies whether the Text property should be interpreted as a literal value or as a calculated expression during binding. When we are done creating the report, select the Application button and select Save to save the Report Definition File. The Designer saves the report definition in XML format, which can be read back into the Designer or directly into a C1Report component. Name this Report Definition File as SampleReport.xml.

Define Collection Object DataSource

Following Class Definition acts as schema definition for the report datasource. Datamembers defined should have same name similar to Text fields set in the label controls in the Report Definition File.


public class Person  
{  
    public string Title { get; set; }  
    public string Name { get; set; }  
    public int  Basic { get; set; }  
    public int Salary { get; set; }  
    public string Department { get; set; }  
    public string Subject { get; set; }  
}  

public class ReportDataSource : ObservableCollection  
{  
     public ReportDataSource()  
         : base()  
     {  
        for (int i = 0; i < 400; i++)  
        {  
           Person p = new Person();  
           p.Name = "Name" + i.ToString();  
           p.Title = "Mr";  
           p.Basic = 10000;  
           p.Salary = 20000;  
           p.Department = "Department" + i.ToString();  
           p.Subject = "Subject" + i.ToString();  
           Add(p);  
         }  
     }  
}  


Loading Report and Assigning DataSource

First drag and drop a DocumentViewer in the XAML definition file.


<Grid>  
  <c1pr:C1DocumentViewer Name="C1DocumentViewer1"/>  
</Grid>  

Include the XML File saved earlier in the Project folder. Then use the following code snippet to load the Report Definition file and assign the datasource.


C1Report rpt = new C1Report();  
rpt.Load(@"..\\..\\SampleReport.xml", "C1Report_ListDataSource");  

//Create Object for ObservableCollection  
ReportDataSource persons = new ReportDataSource();  

rpt.DataSource.Recordset = persons;  
C1DocumentViewer1.Document = rpt.FixedDocumentSequence;  


Final Output for the above implementation appears as shown below. Refer to the attached sample for complete implementation. Download Sample

MESCIUS inc.

comments powered by Disqus