C# 如何使用报表查看器在 asp.net 中创建报表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10222980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:56:50  来源:igfitidea点击:

How to create report in asp.net using Report Viewer

c#asp.netreportviewer

提问by Sonam Mohite

I am using Visual Studio 2010. I have worked on Crystal Reporting before that but now i want to generate reports using Report Viewer. As i am new to this topic please Guide me. Thanks !!!

我使用的是 Visual Studio 2010。在此之前我曾在 Crystal Reporting 上工作,但现在我想使用 Report Viewer 生成报告。由于我是这个主题的新手,请指导我。谢谢 !!!

回答by Bhuvi

My code works to create Report for business class objects...

我的代码用于为业务类对象创建报告...

Creating Report using Business Class Objects & ReportViewer (ASP.NET/ C#) 1.Create Student Class

使用业务类对象和 ReportViewer (ASP.NET/ C#) 创建报表 1.创建学生类

  public class StudentClass
  {
      public int No { get; set; }
      public string Name { get; set; }
      public string Degree { get; set; }
   }

2.Create Student Repository with GetStudents() function

2.使用 GetStudents() 函数创建学生存储库

public class StudentRepository : StudentClass
    {
        public List<StudentClass> studentList = new List<StudentClass>();

        public List<StudentClass> GetStudents()
        {            
            StudentClass student1 = new StudentClass();
            student1.No = 1;
            student1.Name = "Bhuvana";
            student1.Degree = "M.Tech";
            studentList.Add(student1);
            StudentClass student2 = new StudentClass();
            student2.No = 2;
            student2.Name = "Annie";
            student2.Degree = "B.Tech";
            studentList.Add(student2);
            StudentClass student3 = new StudentClass();
            student3.No = 3;
            student3.Name = "Muthu Abi";
            student3.Degree = "B.Tech";
            studentList.Add(student3);
            return studentList;
        }
    }

3.Using Report Wizard create “StudentReport.rdlc” and select DataSource

3.使用报表向导创建“StudentReport.rdlc”并选择DataSource

4.In Index.aspx add Script Manager and Report Viewer from ToolBox(Drag And Drop)

4.在Index.aspx中从ToolBox(拖放)添加脚本管理器和报告查看器

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>  
    <rsweb:ReportViewer ID="ReportViewer1" runat="server">
    </rsweb:ReportViewer>       
</div>

5.Modify Page_Load() method in code behind file

5.修改文件隐藏代码中的Page_Load()方法

public partial class Index : System.Web.UI.Page
{
    StudentRepository sr = new StudentRepository();
    List<StudentClass> sc = new List<StudentClass>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report/Student.rdlc");
            sc = sr.GetStudents();
            IEnumerable<StudentClass> ie;
            ie = sc.AsQueryable();
            ReportDataSource datasource = new ReportDataSource("DataSet1", ie);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
        }

    }
}

6.Build And Run

6.构建和运行