Wednesday 12 February 2014

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.

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.

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

In the next step, select Student class and click on 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 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.

To display the student data in the format of chart choose Insert -> Chart from the context menu as shown below:

Select chart type from variety of options given in the Select Chart Type dialog. Here I am choosing 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 with the report viewer as shown below:

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:

Thanks for reading this article. You can enter your valuable comments and suggestion to improve this article in the comment box.
In order to get the source code of this application, you can enter your valuable comment.

3 comments:

Unknown said...

how to set specific value any row for particular column in bold ??just from ex. how to set "Arun" name in bold or any specific color in c# windows form.

Rohit said...

You can do so by following the steps given below:
1) Right click Name cell and Select Text Box Properties...
2) Now select Font options from the left pane
3) Select Expression button just right towards Bold text.
4) Enter the given condition to bold the specific value:
=IIf(Fields!Name.Value = "Arun", "Bold", "Default")
5) Click OK and you have done it.

Rohit said...

You can do so by following the steps given below:
1) Right click on Name cell and Select Text Box Properties...
2) Now select Font options from the left pane
3) Select Expression button just right towards Bold text.
4) Enter the given condition to bold the specific value:
=IIf(Fields!Name.Value = "Arun", "Bold", "Default")
5) Click OK and you have done it.