c# - 列表中的 Microsoft WinForm ReportViewer

标签 c# winforms reporting reportviewer

谁能提供代码片段、教程链接或有关如何从对象列表在 Microsoft Report 中创建报告的信息?

我有以下 Dog 类:

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

然后,在窗口窗体中,我有一个 ReportViewer 对象,我想从 ListMyNS.Dog 对象中填充它,如下所示:

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

有什么想法吗?

谢谢!

最佳答案

对于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;
        }
    }

构建您的解决方案,将 reportviewer 控件添加到您的表单,在 reportviewer smarttag 上,创建一个新报表并选择对象数据源,展开您的类并选中 Dog 类作为您的对象数据源。再次选择你的 reportviewer 控件,并选择新创建的报表,一个 DogBindingSource 会自动创建。在您的窗体类中,将以下代码添加到类的顶部。您可以在 public partial class Form1 之后使用第一行:Form { 语句,但在构造函数之前

private DogBll _dogBll = new DogBll();

在 formload() 上,添加:

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

对于 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;
      }
    }
} 

添加一个报表查看器向导控件,选择数据源作为您刚刚创建的新函数 GetDogs(),根据 Dog 类中的 3 个公共(public)属性定义您的报表。在您的表单中添加一个对象数据源,指向报表以使用该对象数据源。最后,在对象数据源中设置GetDogs()的参数。

关于c# - 列表中的 Microsoft WinForm ReportViewer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/820514/

相关文章:

C# 网络问题

c# - 如何在C#中检测winform控件外的鼠标点击

c# - 一键式应用程序从 Windows 窗体迁移到 WPF

c# - 即使我关闭了应用程序,我的 winform 应用程序仍在后台运行

java - Flex 用于具有 Java/Oracle 后端的基于图表的报告

r - 如何从 R 生成报告质量表?

c# - WPF 命令绑定(bind)到带有 MVVM 的 DataItemTemplate

c# - 将从文本文件解析的 250 万行数据插入 Sql 服务器的最快方法是什么

c# - 将字符串转换为 datetime2

reporting - 如何在 Birt 设计器中对数据立方体进行切片?