Microsoft WinForm ReportViewer from the list

Can someone provide a link to a piece of code, a link to a tutorial, or information on how to create a report in Microsoft Report from a list of objects?

I have the following Dog class:

namespace MyNS { public class Dog { public int Legs { get; set; } public string Name { get; set; } public string Breed { get; set; } } } 

Then in Window Forms, I have a ReportViewer objetct object that I would like to populate from the list of MyNS.Dog objects:

 List<MyNS.Dog> MyDogs = new List<MyNS.Dog>(); // populate array here // and use it as datasource for ReportViewer 

Any ideas?

Thanks!

+4
source share
2 answers

For a local report, you can specify your data source as follows:

 var reportViewer = New ReportViewer(); var reportDataSource = New ReportDataSource("MyNS_Dog", MyDogs); reportViewer.LocalReport.DataSources.Add(reportDataSource); 
+2
source

For winview reportviewer: include the following code

 public class Dog { int legs; public int Legs { get { return legs; } set { legs = value; } } string name; public string Name { get { return name; } set { name = value; } } string breed; public string Breed { get { return breed; } set { breed = value; } } } public class DogBll { List<Dog> myDog; public DogBll() { myDog = new List<Dog>(); myDog.Add(new Dog() { Legs = 10, Name = "mimi", Breed = "german" }); myDog.Add(new Dog() { Legs = 4, Name = "momo", Breed = "english" }); } public List<Dog> GetDogs() { return myDog; } } 

create your own solution, add a reportviewer control to your form, in smartview from reportviewer, create a new report and select an object data source, expand your class and check the Dog class as an object data source. select the ReportViewer control again and select the newly created report, automatically create the DogBindingSource object. In your form class, add the following code to the top of the class. You can use the first line after the open partial class Form1: Form {statement, but before the constructor

 private DogBll _dogBll = new DogBll(); 

In formload () add:

 this.DogBindingSource.DataSource = _dogBll.GetDogs(); 

For webform reportviewer: you must provide a function that returns a Dog list, in this class it must contain a default constructor.

 namespace MyNS { public class Dog { public int Legs { get; set; } public string Name { get; set; } public string Breed { get; set; } } public class DogBll { public DogBll() { } public List<Dog> GetDogs(List<Dog> myDog)//make sure you set the parameter in object datasource { return myDog; } } } 

add a control for the report view wizard, select the data source as the new function you just created, GetDogs (), define your report based on 3 public properties in your Dog class. Add an object data source to your form, specify a report to use the object data source. Finally, set the GetDogs () parameter in the object's data source.

+4
source

All Articles