none
ReportViewer and Enums RRS feed

  • Question

  • Hi all!
    I have a question on binding an enum to a report.

    Supose you have a database table with a column "severity" where you store a value according to an enum, say

        enum Severity {High, Medium, Low};

    Then you build a Report with the ReportViewer tool, where one of the columns in your .rdlc is bound to the column "severity" of the dataset.
    But then, if you run the report, instead of "High," "Medium" and "Low", you see "0," "1" and "2"!!

    Is there a way to tell the report to treat that field as the type Severity so that it shows the words instead of the values?

    Thank you very much!!
    Alejandro =)
    Thursday, August 13, 2009 1:13 AM

All replies

  • Hello,

    How about putting something like the following in your report custom code (eg Report, Report Properties, Code tab)

    Public Function ConvertEnumValue(ByVal enumValue As Integer) As String
        Try
            If Not [Enum].IsDefined(GetType(YourEnumName), enumValue) Then
                Throw New ArgumentException(String.Format("Invalid enum value '{0}' for '{1}'.", enumValue, GetType(YourEnumName).Name))
            End If
            Return [Enum].GetName(GetType(YourEnumName), value)
        Catch ex As Exception
            Throw
        End Try
    End Function

    public string ConvertEnumValue(int enumValue)
    {
        try {
            if (!Enum.IsDefined(typeof(YourEnumName), enumValue)) 
                throw new ArgumentException(string.Format("Invalid enum value '{0}' for '{1}'.", enumValue, typeof(YourEnumName).Name)); 
            return Enum.GetName(typeof(YourEnumName), value);
        }
        catch (Exception ex) {
            throw;
        }
    }

    Good Luck!

    Thursday, August 13, 2009 4:02 PM
  • Hi!! Thank you very much for answering!! In the end I did something like that, only that as I didn't find a way to reference a project (only dlls, as it seems...) i had to copy the enum code in the Report Custom Code as well...

    Do you know if there's any way to reference a project other than where the report is, so that I don't need to copy the code??

    Thanks in adavance!
    Alejandro =)
    Monday, September 7, 2009 12:31 PM
  • Coming at this from another direction, can you modify your stored procedure to do that for you with a Case statement? Check BOL as I don't use it very often but it should be something along the lines of

    Select Case MyValue When 2 Then 'High' When 1 Then 'Medium' Else 'Low' End Case As ErrorLevel. Good Luck!

    Wednesday, September 9, 2009 2:10 AM
  • Hi ,

    Try this sample enum and class

    public enum EnumType
    {
        TypeA,
        TypeB,
        TypeC
    }
    public class Model
    {
        public Guid ID { get; set; }
        public String Name { get; set; }
        public EnumType Type { get; set; }
    }

    Add Model class to your rdlc as dataset and named to "Model", the "Type" field of "Model" dataset  should be EnumType.

    Apply this expression to your text box of rdlc which need to show the correct enum name.

    =System.Enum.GetName(First(Fields!Type.Value, "Model").GetType(), Fields!Type.Value)



    • Edited by f01senoj Friday, November 8, 2013 4:51 AM
    Friday, November 8, 2013 4:49 AM