Search code examples
c#asp.netdevexpressbooleanaspxgridview

Replace boolean with an image in a ASPXGridView


I have a few columns on my ASPXGridView which contain boolean values.

I want to replace the the values that are 'True' with a tick icon. Then I want to replace the values which are false with a cross icon.

Is there a way of doing this? I already have the icon images for the tick and cross.

Thanks


Solution

  • You can do something like this:

    ASPX:

    <ItemTemplate>
        <asp:Image ID="imgBooleanState" runat="server" AlternateText="State" ImageUrl='<%# GetBooleanState(Eval("MyBooleanValue")) %>' />
    </ItemTemplate>
    

    Code behind:

    protected string GetBooleanState(object state)
    {
        bool result = false;
        Boolean.TryParse(state.ToString(), out result);
        if (result)
        {
             return ResolveUrl("~/path/tick.png");
        }
        else
        {
             return ResolveUrl("~/path/cross.png");
        }
    }