3

I have a gridview with dynamic datasource.

I add column in codebehind .

BoundField col=new BundField(); 
col.DataField="Active";
col.DataFormatString=//DataFormatString;
gridview1.Columns.Add(col);

How do set yes/no for boolean column ?

ar.gorgin
  • 4,765
  • 12
  • 61
  • 100

5 Answers5

1

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>
Andrew
  • 4,443
  • 5
  • 33
  • 75
Jsperk
  • 124
  • 1
  • 11
0

I have found that the solutions above using DataFormatString do not work for me in a GridView.

What worked for me was to use a template column, which I was trying to avoid...

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

Community
  • 1
  • 1
awhig
  • 507
  • 1
  • 7
  • 13
0

I would have done that with SQL, when selecting data as:

select id, fname, (case  when Active = 'true' then 'Yes' else 'No' end) AS Active from tblName

Now no need of formatting it anymore the returned column Active is replaced with another column with the same name but that contains the values of Yes/No.
I assumed your column active is a bit column that contains either true/false, but if something else replace the true/false with your values

Mubarek
  • 2,691
  • 1
  • 15
  • 24
  • But what if you need to use the boolean value before you display it? This method will break that unless you check for a 'yes' string or a 'no' string when you need to use it. Good idea; but only when you're just gonna view the data. – Richard Barker Sep 25 '15 at 21:10
-2

you can do like this

dataformatstring="{0:Yes/No}"
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
-2

use dataformatstring="{0:Yes/No}";

JayOnDotNet
  • 398
  • 1
  • 5
  • 17