Domanda

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

È stato utile?

Soluzione

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");
    }
}

Altri suggerimenti

You will have to check boolean value on rowdatabound and accordingly bind the image as follows:

protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    Image imgCtrl = (Image) e.Row.FindControl("imgCtrl");
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        If(e.Row.DataItem("BooleanField"))
        {
        imgCtrl.ImageUrl = TickImagePath;
        }
        else
        {
        imgCtrl.ImageUrl = CrossImagePath;  
        } 
    }
}

In above code i have simply checked the value of your boolean field on rowdatabound and accordingly seted the image path or decided whether to set "Cross Image path" or set "Tick image" path.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top