Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" (in Danish).

Is this possible?

<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false">
    <columns>
        ...
        <asp:boundfield headertext="Active" datafield="Active" dataformatstring="{0:Yes/No}" />
        ...
    </columns>
</asp:gridview>
like image 419
Thomas Jespersen Avatar asked Oct 19 '08 19:10

Thomas Jespersen


7 Answers

I use this code for VB:

<asp:TemplateField HeaderText="Active" SortExpression="Active">
    <ItemTemplate><%#IIf(Boolean.Parse(Eval("Active").ToString()), "Yes", "No")%></ItemTemplate>
</asp:TemplateField>

And this should work for C# (untested):

<asp:TemplateField HeaderText="Active" SortExpression="Active">
    <ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>
like image 142
travis Avatar answered Nov 18 '22 07:11

travis


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) %>
like image 45
Rune Grimstad Avatar answered Nov 18 '22 07:11

Rune Grimstad


Nope - but you could use a template column:

<script runat="server">
  TResult Eval<T, TResult>(string field, Func<T, TResult> converter) {
     object o = DataBinder.Eval(Container.DataItem, field);
     if (converter == null) {
        return (TResult)o;
     }
     return converter((T)o);
  }
</script>

<asp:TemplateField>
  <ItemTemplate>
     <%# Eval<bool, string>("Active", b => b ? "Yes" : "No") %>
  </ItemTemplate>
</asp:TemplateField>
like image 7
Mark Brackett Avatar answered Nov 18 '22 06:11

Mark Brackett


You could use a Mixin.

/// <summary>
/// Adds "mixins" to the Boolean class.
/// </summary>
public static class BooleanMixins
{
    /// <summary>
    /// Converts the value of this instance to its equivalent string representation (either "Yes" or "No").
    /// </summary>
    /// <param name="boolean"></param>
    /// <returns>string</returns>
    public static string ToYesNoString(this Boolean boolean)
    {
        return boolean ? "Yes" : "No";
    }
}
like image 6
Corey Coto Avatar answered Nov 18 '22 06:11

Corey Coto


Or you can use the ItemDataBound event in the code behind.

like image 3
Paco Avatar answered Nov 18 '22 06:11

Paco


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"))%>
like image 2
Shaun3180 Avatar answered Nov 18 '22 06:11

Shaun3180


This is how I've always done it:

<ItemTemplate>
  <%# Boolean.Parse(Eval("Active").ToString()) ? "Yes" : "No" %>
</ItemTemplate>

Hope that helps.

like image 1
Chtiwi Malek Avatar answered Nov 18 '22 08:11

Chtiwi Malek