문제

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

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top