Problem: I have an enumerated type which has description tags in the following style: description tag tutorial . I have a Windows SQL Server Database from which I am pulling the data (as integers then castine to Enums) which is then being bound to a datagrid. Instead of pulling and casing the enumerated types, I would like to display the description tag associated with it in the enumerated type.

Here is the ASP -

<asp:GridView ID="StatementGrid" runat="server" AutoGenerateColumns="false" DataKeyNames="statementID" OnRowDeleting="StatementGrid_onDeleting" AllowSorting="False">
                    <asp:BoundField HeaderText="Type" SortExpression="type" DataField="TypeOfStatement" />
                    <asp:HyperLinkField HeaderText="Statement" DataTextField="StatementText" DataNavigateUrlFormatString="~/Gateway/Statements/View.aspx?statementID={0}" SortExpression="statement" DataNavigateUrlFields="statementID" />
                    <asp:HyperLinkField DataNavigateUrlFields="statementID" DataNavigateUrlFormatString="~/Gateway/Statements/Update.aspx?statementID={0}" NavigateUrl="~/Gateway/Statements/Update.aspx" HeaderText="Edit" Text="<img src='../../Images/News/news_edit.gif' alt='Edit Statement'/>" />
                    <asp:TemplateField HeaderText="Delete">
                            <asp:ImageButton AlternateText="Delete Statement" ID="DeleteButton" ImageUrl="~/Images/News/news_delete.gif" runat="server" CommandName="Delete" OnClientClick="javascript:return confirm('Are you sure you want to delete this statement?');" />
                    There are no statements to display.

Here is the code for the Bind -

private void BindData()
            IStatementDao statementDao = DaoFactory.GetStatementDao();
            List<Statement> statements;

            if (Page.Request.RawUrl.Contains("Gateway"))
                statements = statementDao.GetAll();

                StatementGrid.HeaderStyle.CssClass = "GatewayGridHeader";
                StatementGrid.AlternatingRowStyle.CssClass = "GatewayGridAlternatingRow";

                // This should never be reached but it keeps the compiler happy!!
                statements = statementDao.GetAll();

            StatementGrid.DataSource = statements;

Here is the enumerated class -

public enum TypeOfStatement 
            [EnumDescription("Dress Code")] DressCode = 1,
            [EnumDescription("Lunch Time")] LunchTime = 2,
            [EnumDescription("Footwarez")] Footware = 3,
            [EnumDescription("achtung")] Warning = 4,
            [EnumDescription("Banarna")] Banana = 5,
            [EnumDescription("Apfel")] Apple = 6

Its obvious that one could write an extensive method do do what i want, but is there a neater way?

First off why not use the description attribute provided in System.ComponentModel? It doesn't make sense to create an attribute for something that already exists:

Second can't you handle the RowDataBound event to substitute the text? See

Since you use reflection to get the description from the enumeration member you are probably better off creating a Dictionary<int index, string Description> and fetching the values from there instead of firing off the reflector for every row.

Unfortunately you have the right approach with using another method to get the values in there and that should be the recommended way with the information you have provided.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.