If condition on a gridview for boundfield?

software picture software · May 1, 2013 · Viewed 12.7k times · Source

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>

Answer

Zo Has picture Zo Has · May 2, 2013

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.