---
title: "Using ReportViewer in WinForms C#"  
description: "In this article, I am going to explain how to dynamically create a report by using a report viewer in a windows form."  
author: "Chris Anderson"  
published: 2013-01-21  
updated: 2020-07-06  
canonical: https://www.mindstick.com/articles/1118/using-reportviewer-in-winforms-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 6 minutes  

---

# Using ReportViewer in WinForms C#

## Using ReportViewer in WinForms C#

Today in this Article, I am trying to explain how to dynamically create a report by using report viewer in a 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

Using ReportViewer in WinForms C#

- 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#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/5b9323eb-c3b4-4bda-bc35-511a681fa1ff.png)

This example also uses the following class (Student) and its properties and methods to display the data in the report. [Please create or include](https://www.mindstick.com/articles/1109/fetch-data-from-entity-framework-using-asp-dot-net-mvc4) 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#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/9ac81b15-b573-48ad-b993-f5806a83387c.png)

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

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/e53752cb-c9ea-4f2a-bbea-87c6122455cf.png)

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

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/6b28d26d-7b0a-46ce-8cd9-b070b1a296d2.png)

- Add a new dataset in StudentReport.rdlc page and name the dataset as StudentDS.
- Choose Student from the Available dataset options.
- Click on the OK button to finish the step.

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/3db4d184-e04e-430a-9b1f-566d1b984b0a.png)

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

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/9ef65912-6d95-4211-aa67-dc71d0ff4227.png)

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

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/e4647f50-f007-49e0-85e2-cfc216bd9011.png)

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

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/881109fe-582f-4d79-a83c-06f80eac0eec.png)

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#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/d40a8819-00fa-421e-8574-2a0609f04391.png)

Now bind StudentReport.rdlc

![Using ReportViewer in WinForms C#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/8259ac76-6657-4ac3-b476-989a5a37f39b.png)

The following code example will render the Student [report in the report viewer](https://www.mindstick.com/Articles/1473/the-ajax-helper-tools-and-controls-in-asp-dot-net-mvc-4).

```
        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#](https://www.mindstick.com/mindstickarticle/13169999-ef3b-496c-b502-caef973c3bb2/images/baa4e0cf-82ae-4a87-aaac-07dbd08ea0d1.png)

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.

---

Original Source: https://www.mindstick.com/articles/1118/using-reportviewer-in-winforms-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
