Search code examples
c#reporting-servicescrystal-reportsdevexpress

How to create a report and bound it to an object using devExpress ReportWizard


I have a simple class. I want to create a report with this class. this is my class:

public class report
{
    public string userName { get; set; }
    public string userVardNo { get; set; }
    public string userMobile { get; set; }
    public string userBirthDay { get; set; }
    public int totalHours { get; set; }
    public int totalMinutes { get; set; }
    public int totalDays { get; set; }
    public string monthName { get; set; }
    public string reportDateTime { get; set; }
    public string totalPrice { get; set; }
    public string pricePerHour { get; set; }
}

And this is how I create a report step by step:

Project->Add New Item->DevExpress v X.X Report Wizard-> then this dialogue opens: enter image description here

I choose Data Bound Report. then : enter image description here

I choose Object Binding. then I choose my report class and then choose retrieve data source schema.(I tried both but in vain)

enter image description here

Then I choose all the fields and so on. every thing is ok. i design my report and close it. enter image description here

then I create a form. add a document viewer to it. and then in my Form constructor class I write these lines:

public report_form()
    {
        InitializeComponent();
        report report_class = new report();
        report_class.userName = "Soup MacTavish";report_class.userMobile = "555-987654";//And so on...
        XtraReport1 report_1 = new XtraReport1();
        report_1.DataSource = report_class;
        documentViewer1.DocumentSource = report_1;
        documentViewer1.Refresh();
    }

i run my program but no data is visible. i just get this error:

enter image description here

I change my report class to inherit the data source interface I used in my report like this:

public class report: DevExpress.DataAccess.ObjectBinding.ObjectDataSource
{
    public string userName { get; set; }
    public string userVardNo { get; set; }
    public string userMobile { get; set; }
    public string userBirthDay { get; set; }
    public int totalHours { get; set; }
    public int totalMinutes { get; set; }
    public int totalDays { get; set; }
    public string monthName { get; set; }
    public string reportDateTime { get; set; }
    public string totalPrice { get; set; }
    public string pricePerHour { get; set; }
}

this time error is gone but no data is visible.

enter image description here

how can I create a report that is bound to a class?


Solution

  • At first, i would recommend you to use the Microsoft Styleguide. So write Classnames Uppercase (Report) and so on Microsoft C# Codeconventions .

    But now to your problem. As far as i know you have to use a List. BindingList, ReadOnlyCollection etc. works as well, but let's make it simple. Try following code for DataBinding:

    List<Report> dummyList = new List<Report>();
    dummyList.Add(new Report());
    XtraReport myReport = new XtraReport();
    myReport.DataSource = dummyList;
    

    This should work for you. Your class don't need to implement any interface.