Can I convert a boolean to Yes/No in a ASP.NET GridView

前端 未结 9 1302
孤街浪徒
孤街浪徒 2020-12-22 17:17

I have a ASP.NET GridView with a column mapped to a boolean. I want do display \"Yes\"/\"No\" instead of \"True\"/\"False\". Well actually I want \"Ja\"/\"Nej\"

9条回答
  •  清酒与你
    2020-12-22 17:45

    I had the same need as the original poster, except that my client's db schema is a nullable bit (ie, allows for True/False/NULL). Here's some code I wrote to both display Yes/No and handle potential nulls.

    Code-Behind:

    public string ConvertNullableBoolToYesNo(object pBool)
    {
        if (pBool != null)
        {
            return (bool)pBool ? "Yes" : "No";
        }
        else
        {
            return "No";
        }
    }
    

    Front-End:

    <%# ConvertNullableBoolToYesNo(Eval("YOUR_FIELD"))%>
    

提交回复
热议问题