1

I have a gridview with the following boundfiled. DataField is decimal value. If the value is anything higher than 0 I want to display as True in grdiview else false. How to do , if conditon for the boundfileld . can anyone help. I can use TemplateField if that gives the solution.

<asp:BoundField HeaderText="fieldone" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
    DataField="Higher" NullDisplayText="0">
    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
    <ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="fieldtwo" Rebate" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
    DataField="Lower" NullDisplayText="0">
    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
    <ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundField>
Damith
  • 62,401
  • 13
  • 102
  • 153
software
  • 728
  • 6
  • 18
  • 39
  • Any reason that you can't use template field? or can't you change data source before binding? – Damith May 01 '13 at 13:42
  • I can use template field. – software May 01 '13 at 13:43
  • [Evaluate datafield on boundfield to display text accordingly](http://stackoverflow.com/questions/13941184/evaluate-datafield-on-boundfield-to-display-text-accordingly) – Damith May 01 '13 at 13:44
  • @Damith: What is ItemStyle-CssClass="TemplateFieldOneColumn? – software May 01 '13 at 13:54
  • The following code should in ascx page or ascx.vb page? I use vb.net.public string GetLabelText(object dataItem) { string text = ""; int? val = dataItem as int?; switch (val) { case 1: text = "Pending"; break; case 2: text = "Deleted"; break; } return text; } – software May 01 '13 at 15:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29251/discussion-between-damith-and-software) – Damith May 01 '13 at 15:09
  • I should have 20 reputation to talk here – software May 01 '13 at 15:16

1 Answers1

4

You can use a templatefield & drop a label control inside it. Something like

<asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblStatus" runat="server" Text='<%# (Convert.ToDecimal(Eval("UnitPrice")) > 0) ? "True" : "False"   %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

Alternatively, you can also use RowDataBound event of gridview & use FindControl to apply the same.

Zo Has
  • 12,599
  • 22
  • 87
  • 149
  • when I use this code, In server page I get error " The '?" character cannot be used here." – software May 06 '13 at 17:14
  • @software That works for me. . You may be missing an enclosing quotation mark. Please ensure all server side code is enclosed properly inside single quotation marks'<%# your code goes here %>' – Zo Has May 07 '13 at 04:52