C# 来自 List 的 Microsoft WinForm ReportViewer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/820514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microsoft WinForm ReportViewer from List
提问by Pablo Santa Cruz
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 Report 中创建报告的信息?
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 Listof MyNS.Dogobjects like this:
然后,在窗口窗体中,我有一个 ReportViewer 对象,我想从这样的MyNS.Dog对象列表中填充它:
List<MyNS.Dog> MyDogs = new List<MyNS.Dog>();
// populate array here
// and use it as datasource for ReportViewer
Any ideas?
有任何想法吗?
Thanks!
谢谢!
采纳答案by Meta-Knight
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);
回答by gavin
For winform reportviewer: include the following code
对于 winform 报表查看器:包括以下代码
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
构建您的解决方案,在您的表单中添加一个报表查看器控件,在报表查看器智能标签上,创建一个新报表并选择对象数据源,展开您的类并检查 Dog 类作为您的对象数据源。再次选择您的报表查看器控件,并选择新创建的报表,会自动创建一个 DogBindingSource。在您的表单类中,将以下代码添加到类的顶部。您可以在公共部分类 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. 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(),根据 Dog 类中的 3 个公共属性定义您的报告。在表单中添加对象数据源,将报表指向使用对象数据源。最后,在对象数据源中设置GetDogs()的参数。

