tags:

views:

497

answers:

4

In a GridView, I'm trying to show either one column or an other using a public Boolean property:

   <Columns>
   ...

    <asp:BoundField Visible="<%= !ShowPurchaseDate %>" DataField="Published" HeaderText="Publicerad" SortExpression="PriceRange" DataFormatString="{0:yyyy.MM.dd}" HtmlEncode="false" />

    <asp:BoundField Visible="<%= ShowPurchaseDate %>" DataField="OrderDate" HeaderText="Köpt" SortExpression="OrderDate" DataFormatString="{0:yyyy.MM.dd}" HtmlEncode="false" />

   ...
  </Columns>

But I'm getting an error message saying that it's not possible to create a System.Boolean from a string that contains <% !ShowPurchaseDate %> for property Visisble.

How can I achive this?

UPDATE:

<%# !ShowPurchaseDate %> doesn't work either as there's no databinding going on.

Using a protected funktion doesn't work either (same error message as with property).

A: 

Provide a protected method in the code behind that returns the boolean property as a string.

Dan
A: 

You're code should be <%# !ShowPurchaseDate %>

Albert
Who gave -1? The answer is correct... DataBound field does require usage of <%# %> and not <%= %>
Ruslan
I forgot to add, that the BoundField itself is not a data-bound control, so template would be required.
Ruslan
+1  A: 

I solved it through:

gridMain.Columns[ShowPurchaseDate ? 3 : 4].Visible = false;

Is this the best solution?

Niels Bosma
this is what I typically do.
Ben Scheirman
That's not the best solution. It is based on indexes that change all the time. Make it a template field and use the data-binding approach I suggested.
Albert
With a template field, you can make the controls in the column disappear but not the whole column. So, I agree that it needs to be done in the code behind.
EfficionDave
A: 

you could try calling a method instead of binding like shouldshowcolumn().