简体   繁体   English

来自List的Microsoft WinForm ReportViewer

[英]Microsoft WinForm ReportViewer from List

Can anyone provide a Code Snippet, Tutorial link or information on how to create a report in Microsoft Report from a List of objects? 任何人都可以提供代码片段,教程链接或有关如何从对象列表在Microsoft报表中创建报表的信息?

I have the following Dog class: 我有以下Dog类:

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: 然后,在Window Forms中,我有一个ReportViewer对象,我想从MyNS.Dog 列表中填充这样的对象:

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

Any ideas? 有任何想法吗?

Thanks! 谢谢!

For winform reportviewer: include the following code 对于winform reportviewer:包含以下代码

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. 构建解决方案,向表单添加reportviewer控件,在reportviewer smarttag上创建新报表并选择对象数据源,展开类并检查Dog类作为对象数据源。 select your reportviewer control again, and choose the newly created report, a DogBindingSource is automatically created. 再次选择您的reportviewer控件,并选择新创建的报表,自动创建DogBindingSource。 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 您可以在公共部分类Form1:Form {语句之后但在构造函数之前使用第一行

private DogBll _dogBll = new DogBll();

On formload(), add: 在formload()上,添加:

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. 对于webform reportviewer:你应该提供一个返回Dog列表的函数,在这个类中它应该包含一个默认的构造函数。

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. 添加报表查看器向导控件,选择数据源作为刚创建的新函数,GetDogs(),根据Dog类中的3个公共属性定义报表。 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. 最后,在对象数据源中设置GetDogs()的参数。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM