I am trying to pro grammatically alter a value in a GridView based on a returned database value.
The database value "is_zero_minutes_task" is Boolean. If True I wish to display a "0" and if False I wish to display the value returned in "MinutesTaken".
My code:
<asp:SqlDataSource ID="SqlDataSourceRecentJobs" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support %>"
ProviderName="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support.ProviderName %>"
SelectCommand="SELECT customer_name, start_time, end_time, is_zero_minutes_task, TIMESTAMPDIFF(MINUTE,start_time,end_time) AS MinutesTaken FROM time_recorder_jobs WHERE (time_recorder_jobs.deleted = @deleted) AND (time_recorder_users.company_id = @companyid) ORDER BY end_time">
<SelectParameters>
<asp:Parameter Name="@deleted" DefaultValue="0" Type="Object" />
<asp:SessionParameter Name="companyid" SessionField="CompanyID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridViewRecentJobs" runat="server"
AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSourceRecentJobs" ForeColor="#333333"
GridLines="None" CellSpacing="4" ShowHeaderWhenEmpty="True"
AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="customer_name" HeaderText="customer_name"
SortExpression="customer_name">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="start_time" HeaderText="start_time"
SortExpression="start_time">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="end_time" HeaderText="end_time"
SortExpression="end_time">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
<ItemTemplate>
<%If Eval("is_zero_minutes_task").ToString = True Then%>
<asp:Label ID="MinutesTaken" runat="server" Text="0"></asp:Label>
<%Else%>
<asp:Label ID="MinutesTaken" runat="server" Text='<%# Bind("MinutesTaken") %>'></asp:Label>
<%End If%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also tried using:
<%If DataBinder.Eval(GridViewRecentJobs.DataItem, "is_zero_minutes_task") = True Then%>
But that presents the error of "'DataItem' is not a member of System.Web.UI.WebControls.GridView'"
I worked it out!
<asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
<ItemTemplate>
<asp:Label ID="MinutesTaken" runat="server" Text='<%# If((Eval("is_zero_minutes_task") = 1), "0", Eval("MinutesTaken"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>