C# – Using Crystal Reports with a C# Object

ccrystal-reportslist

I am new to Crystal reports. I need to build a Report but don't have access to Data Source. The reports need to be populated with a .Net Class containing a List of Data. How can i do this. I have been searching on Google most of the day but can only find examples of using a SQL or ADO.NET data source. How can i do this using a Class Object with a List in C#.

Kind Regards.

Mateen

Best Answer

We can use .Net Class as a DataSource For Crystal Report While Adding Crystal Report it will ask for Ad0.Net or .Net Object. In that we have to Choose .Net Objects. it would list all the classes in your project. in That we can choose the class which we want to use it as a datasource. Then the properties of the Class will be listed and in that we can choose the necessary fields

Here is the Sample Code:

Dy_Daywisesales_RPT myDataReport = new Dy_Daywisesales_RPT();

// This is to get the individual textobject
(myDataReport.Section2.ReportObjects["txtFromDate"] as TextObject).Text=Daywisesales.From1;
(myDataReport.Section2.ReportObjects["txtToDate"] as TextObject).Text=Daywisesales.To1;
(myDataReport.Section2.ReportObjects["txtStation"] as TextObject).Text=Daywisesales.StationName;

// This is the collection as a datasource
myDataReport.SetDataSource(Daywisesales.lstDayWiseSalesDetails);
crystalReportViewer1.ReportSource = myDataReport;