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

前端 未结 9 1236
孤街浪徒
孤街浪徒 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:41

    Add a method to your page class like this:

    public string YesNo(bool active) 
    {
      return active ? "Yes" : "No";
    }
    

    And then in your TemplateField you Bind using this method:

    <%# YesNo(Active) %>
    
    0 讨论(0)
  • 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"))%>
    
    0 讨论(0)
  • 2020-12-22 17:48

    It's easy with Format()-Function

    Format(aBoolean, "YES/NO")
    

    Please find details here: https://msdn.microsoft.com/en-us/library/aa241719(v=vs.60).aspx

    0 讨论(0)
提交回复
热议问题