Thursday, June 9, 2016

ReportViewer RDLC - show enum's string representation

Problem
I have a report that has a data source set to a collection of business objects. One of the fields shown on the report is called "Batch Status." This is an enum. On the report though, it's appearing as the integer value of the enum instead of the string representation.

For example:
BatchStatus.Available is appearing as 1 instead of Available

Solution
In the field express put just put
=System.Enum.GetName(First(Fields!BatchStatus.Value).GetType(), Fields!BatchStatus.Value)

Ref: https://social.msdn.microsoft.com/forums/en-US/a45a3786-5bf7-4661-91bc-587e0b43e849/reportviewer-and-enums

6 comments:

  1. Thank you! It's working :)

    ReplyDelete
  2. It's not working for me :C

    this gives me Int32, First(Fields!TipoRelacion.Value, "DataSetName").GetType()
    And this gives me the position of the enum (3), First(Fields!TipoRelacion.Value, "DataSetName")

    The whole expresion gives me a compilation error.
    =System.Enum.GetName(First(Fields!TipoRelacion.Value, "DataSetName").GetType(), First(Fields!TipoRelacion.Value, "DataSetName"))

    ReplyDelete
  3. First(Fields!TipoRelacion.Value, "DataSetName").GetType() should be returning your enum type "TipoRelacion", not Int32. This is the root problem. The compile error is due to calling Enum.GetName on an integer instead of an enum.

    Please verify:
    1. TipoRelacion is an enum. Paste your code for this (with changed field names if you want)
    2. In the rdlc in the DataSet the type of TipoRelacion is "TipoRelacion".

    ReplyDelete
  4. Hi,

    It doesn't work on my end. My enum is correct but when I get the value type it became an int32.

    Dataset = http://prntscr.com/fvwqvl
    Expression = = First(Fields!InvoiceType.Value, "invoiceDetailDataset").GetType().ToString()

    I hope you could help me with it.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. =First(Fields!InvoiceType.Value, "invoiceDetailDataset").GetType().ToString() SHOULD be returning "InvoiceType", not "Int32". There's something wrong in the business object, dataset, or expression. I put together a simple example here. Please compare this to your own code. If it's still not working for you then please copy and paste the business object, dataset, and full expression you're using for the field on the report


      Business Object
      public enum InvoiceType
      {
      Sale = 1,
      Return = 2
      }
      public class Invoice
      {
      public InvoiceType InvoiceType
      {
      get { return InvoiceType.Sale; }
      }
      }



      RDLC DATASET
      The rd:TypeName is WindowsFormsApplication3.InvoiceType

      RDLC Expression
      =System.Enum.GetName(First(Fields!InvoiceType.Value).GetType(), Fields!InvoiceType.Value)


      When i run this, it's showing "Sale" on the report

      Delete

There was an error in this gadget