Question: I'm trying to find the most effective pattern for binding a control in an ASP.Net form to a database tables that contains a couple of enums as integer values. For example, if I have these enums defined in code:

```
[Flags]
public enum VisibleTo
{
    None = 0,
    Customers = 1,
    Employees = 2,
    Managers = 4
}

public enum LinkType
{
    AdorableCatVideo = 0,
    Selfie = 1,
    FakeNews = 2,
    Pornography = 3        
}

```

Then a row in my database table might look something like this:

[![sample SQL row](https://i.stack.imgur.com/JYAju.png)](https://i.stack.imgur.com/JYAju.png)

And based on that, I would like to be able to use a control like a DetailsView with a SQLDataSource to display a grid showing:

```
Link Text: Kitten Playing With Yarn
Link URL: http://www.kittens.com/yarn134323.html
Link Type: AdorableKittenVideo (preferably with spaces)
Visible to Customers: [check]
Visible to Employees: [check]
Visible to Managers: [check]

```

I have looked into a few control types, and haven't found anything that would make this easy to accomplish, nor have I found any suggestions on Google. The best approach I can think of is to query the database in the Page\_Load method, use a helper method to convert the Link Type int to a human-readable string, and break out the VisibleTo flags into three booleans, and load all of this into a separate DataTable that I can bind to, and then to do some more conversions before updating or inserting.

Can anybody think of a more elegant way to accomplish this? I'm thinking of how easy it is to do this type of thing in WPF and XAML using an IValueConverter, but I'm not as well versed in ASP.Net. Thanks in advance!

Answer: 
You can cast the value from the database directly to the Enum and get it's string value.

```
<asp:TemplateField HeaderText="Link Type">
    <ItemTemplate>
        <%# (LinkType)Convert.ToInt32(Eval("LinkType")) %>
    </ItemTemplate>
</asp:TemplateField>

```
What I do is:

```
[Flags]
public enum VisibleTo
{
    [Description("None")]
    None = 0,
    [Description("Customers")]
    Customers = 1,
    [Description("Employees")]
    Employees = 2,
    [Description("Managers")]
    Managers = 4
}

```

Then I have this

```
public static string GetEnumDescription(Enum en)
{
    var type = en.GetType();
    var memInfo = type.GetMember(en.ToString());

    if (memInfo.Length > 0)
    {
        var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attrs.Length > 0)
        {
            return ((DescriptionAttribute)attrs[0]).Description;
        }
    }

    return en.ToString();
}

```

So then you can do

```
GetEnumDescription((VisibleTo)myIntValue);

```

If you want to go through the list you can do something like this:

```
var visibleCheck = (VisibleTo)intFromDb;
foreach (var visibleItem in new[] { VisibleTo.Customers, VisibleTo.Employees, VisibleTo.Managers })
{
    if(visibleCheck.HasFlag(visibleItem) 
    {
        Console.WriteLine($"{GetEnumDescription(visibleItem)}: CHECKED");
    }
}

```