Search code examples
asp.netgridviewboundfield

How to put condition in grid view bound field?


here my code-

<asp:BoundField DataField="DayOfTheWeek" HeaderText="Day" ItemStyle-CssClass="Itemstyle"/>

from my collection entity I am getting DayOfTheWeek like 1,2...7. 1 for monday , 2 for tuesday like that. Where should I place condition so that in grid view it would display day name rather than corresponding code.


Solution

  • You can use Enum.Parse on the DayOfWeek enum to get the day text back in a TemplateField:

    <asp:TemplateField HeaderText="Day" ItemStyle-CssClass="Itemstyle">
        <ItemTemplate>
            <%# Enum.Parse(typeof(DayOfWeek), DataBinder.Eval(Container.DataItem, "DayOfTheWeek").ToString()) %>
        </ItemTemplate>
    </asp:TemplateField>