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.
This example also uses the following class (Student) and its properties and methods to display the data in the 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.
In Data Source Configuration wizard choose a Data Source Type as Object
In the next step, select the Student class and click on the Finish button.
- 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.
Insert a Table in the rdlc page as shown in the figure below:
Drag and drop the dataset fields into the inserted table columns.
Select chart type from a variety of options given in the Select Chart Type dialog. Here I am choosing a default chart type.
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.
Now bind StudentReport.rdlc
The following code example will render the Student report in the 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:
Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.
Anonymous User
17-Sep-2019This is really a knowledgeable Article...!
Anonymous User
13-Feb-2019Good Article.