ReportViewer RDLC – How to show an enum’s string representation

Problem

I’m building a report using ReportViewer. I’m setting the Data Source to a collection of model objects. One of the fields on the model is an enum. When the report renders, it is showing the enum’s numeric value. I want it to show the string representation of the enum instead.

Solution

In the field I put the following expression:

=System.Enum.GetName(First(Fields!MPAARating.Value).GetType(), Fields!MPAARating.Value)

Here’s what it looks like in the report design:

And this is what the report looks like now:

Code

frmReportViewer.cs

using System;
using System.Windows.Forms;

namespace ReportViewerRDLC
{
    public partial class frmReportViewer : Form
    {
        public frmReportViewer()
        {
            InitializeComponent();
        }

        private void frmReportViewer_Load(object sender, EventArgs e)
        {
            var movies = new MovieCollection()
            {
                new Movie()
                {
                    Name = "Pulp Fiction",
                    MPAARating = MPAARating.R

                },
                new Movie()
                {
                    Name = "The Dark Knight",
                    MPAARating = MPAARating.PG13
                }
            };
            this.MovieCollectionBindingSource.DataSource = movies;
            this.rvMovies.RefreshReport();
        }
    }
}

Movie.cs – the model

namespace ReportViewerRDLC
{
    public class Movie
    {
        public string Name { get; set; }
        public MPAARating MPAARating { get; set; }
    }
}

MovieCollection.cs – the model binding list

using System.ComponentModel;

namespace ReportViewerRDLC
{
    public class MovieCollection : BindingList<Movie>
    {
    }
}

MPAARatingEnum.cs – the enum

namespace ReportViewerRDLC
{
    public enum MPAARating
    {
        G,
        PG,
        PG13,
        R
    }
}

Leave a Comment