Question

Can anyone provide a Code Snippet, Tutorial link 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 which I would like to populate from a List of MyNS.Dog objects like this:

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

Any ideas?

Thanks!

Was it helpful?

Solution

For a local report, you can specify your data source like this:

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

OTHER TIPS

For winform 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;
        }
    }

build your solution, Add a reportviewer control to your form, on the reportviewer smarttag, create a new report and choose object datasource, expand your class and check the Dog class as your object datasource. select your reportviewer control again, and choose the newly created report, a DogBindingSource is automatically created. In your form class, add the following code to the top of the class. You can use the first line after the public partial class Form1 : Form { statement, but before the constructor

private DogBll _dogBll = new DogBll();

On formload(), add:

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

For webform reportviewer: You should provide a function that will return a list of Dog, in this class it should 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 report viewer wizard control, select the datasource as the new function you just created, GetDogs(), define your report based on the 3 public properties in your Dog class. Add an object datasource in your form, point the report to use the object datasource. Finally, set the parameter of GetDogs() in the object datasource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top