Home > DeveloperSection > Articles > Using ReportViewer in WinForms C#

Using ReportViewer in WinForms C#


C# C# 
Ratings:
0 Comment(s)
 89564  View(s)
Rate this:

Using ReportViewer in WinForms C#

In this article I am going to explain how to dynamically create a report by using report viewer in windows form. To view reports that exist on local file system, you can use the WinForms ReportViewer control to render them in a Windows application.

The following example demonstrates how to render a report using ReportViewer control.

To add the ReportViewer Control to a Windows application

·         Create a new Windows application using Microsoft Visual C#.

·         Locate the ReportViewer control in the Toolbox.

·         Drag the ReportViewer control onto the design surface of the Windows Form. A ReportViewer control named reportViewer1 is added to the form.

Using ReportViewer in WinForms C#

This example also uses the following class (Student) and its properties and methods to display the data in report. Please create or include the following class and its content in your application.

    public class Student

    {

        public int StudentID { get; set; }

        public string Name { get; set; }

        public DateTime DateofBirth { get; set; }

        public string Address { get; set; }

        public int Marks { get; set; }

    }

 

    public class StudentRepository

    {

        public static List<Student> GetStudents()

        {

            List<Student> list = new List<Student>

            {

                new Student

                {

                    StudentID = 1,

                    Name = "Rohit",

                    Address = "Uttar Pradesh, Allahabad",

                    Marks = 90,

                    DateofBirth = Convert.ToDateTime("4-Feb-1991")

                },

                new Student

                {

                    StudentID = 2,

                    Name = "Rahul",

                    Address = "Uttar Pradesh, Kanpur",

                    Marks = 85,

                    DateofBirth = Convert.ToDateTime("21-Oct-1991")

                },

                new Student

                {

                    StudentID = 3,

                    Name = "Rati",

                    Address = "Uttar Pradesh, Varanasi",

                    Marks = 80,

                    DateofBirth = Convert.ToDateTime("21-Dec-1991")

                },

                new Student

                {

                    StudentID = 4,

                    Name = "Shweta",

                    Address = "Uttar Pradesh, Allahabad",

                    Marks = 75,

                    DateofBirth = Convert.ToDateTime("21-Nov-1991")

                },

                new Student

                {

                    StudentID = 5,

                    Name = "Arun",

                    Address = "Uttar Pradesh, Lucknow",

                    Marks = 70,

                    DateofBirth = Convert.ToDateTime("3-Mar-1989")

                }

            };

 

            return list;

        }

    }

 

To add the Student details report to a Windows application

·         From the project menu, select Add New Item.

·         Select Report and edit the name and click the Add button. The StudentReport.rdlc file should now be part of the project.

Using ReportViewer in WinForms C#

In Data Source Configuration wizard choose a Data Source Type as Object

Using ReportViewer in WinForms C#

In the next step, select Student class and click on Finish button.

 

Using ReportViewer in WinForms C#

··         Add a new dataset in StudentReport.rdlc page and name the dataset as StudentDS.

·         Choose Student from the Available dataset options.

·         Click on OK button to finish the step.

Using ReportViewer in WinForms C#

Insert a Table in the rdlc page as shown in the figure below:

Using ReportViewer in WinForms C#

Drag and drop the dataset fields into the inserted table columns..

 

Using ReportViewer in WinForms C#

Select chart type from variety of options given in the Select Chart Type dialog. Here I am choosing default chart type.

Using ReportViewer in WinForms C#

After adding a chart, do the following steps to display the student data in chart format::

·         Drag the Sum field from the dataset and drop it into the data field of Chart.

·         Drag the Name field from the dataset and drop it into the Category field of Chart.

·         Edit the X axis title to Name and Y axis title to Marks in order to make both the axis meaningful.

 

Using ReportViewer in WinForms C#

Now bind StudentReport.rdlc

Using ReportViewer in WinForms C#

The following code example will render the Student report in report viewer..

 

        private void Form1_Load(object sender, EventArgs e)

        {

            List<Student> list = StudentRepository.GetStudents();  //get list of students

           

            reportViewer1.LocalReport.DataSources.Clear(); //clear report

            reportViewer1.LocalReport.ReportEmbeddedResource = "Student_ReportViewer.StudentReport.rdlc"; // bind reportviewer with .rdlc

 

            Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("StudentDS", list); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(dataset);

            dataset.Value = list;

 

            reportViewer1.LocalReport.Refresh();

            reportViewer1.RefreshReport(); // refresh report

        }

 

Now run or debug this application to see the output:

Using ReportViewer in WinForms C#

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box..


Don't want to miss updates? Please click the below button!

Follow MindStick