2

i have a dataview with:

<asp:BoundField DataField="AccontoAutorizzato" HeaderText="Acconto Aut." 
                        SortExpression="AccontoAutorizzato" dataformatstring="{0:C}"  />

is possible hide the values of each with a condition like

 Visible=<%# ((Int32)Eval("StatoID") < 2) %>

?

Thanks

Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157

2 Answers2

2

It's possible with following

<asp:TemplateField HeaderText="Acconto Aut." >
     <ItemTemplate>
          <asp:Label ID="lbl" runat="server" Text='<%# Bind("AccontoAutorizzato") %>'
                        Visible='<%# ((int)(Eval("StatoID")) < 2) %>' />
     </ItemTemplate>
</asp:TemplateField>
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
Saar
  • 8,286
  • 5
  • 30
  • 32
  • I am surprised this worked. I have another alternative answered below. – Fandango68 Mar 23 '17 at 00:17
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Mar 23 '17 at 08:34
  • Right @TobySpeight! The solution is to actually avoid using the BoundField because it does not support binding for the visibility. TemplateField however, does. – Marcel May 17 '18 at 13:39
0

Saar's answer did not work for me, because even though the binding should return a true or false, the interpreter could not actually convert the condition result to a Boolean value.

So instead, I used an explicit choice of Boolean values:

<asp:TemplateField HeaderText="Acconto Aut." >
     <ItemTemplate>
          <asp:Label ID="lbl" runat="server" Text='<%# Bind"AccontoAutorizzato") %>'
                        Visible='<%# ((int)(Eval("StatoID")) < 2) ? Convert.ToBoolean(0) : Convert.ToBoolean(1) %>' />
     </ItemTemplate>
</asp:TemplateField>

I hope this makes it easier for others struggling with the Boolean error when applying it to a Visible property.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Fandango68
  • 4,461
  • 4
  • 39
  • 74