Search code examples
c#asp.netlistviewitemdatabound

ListView sum column total and display in label returns null


I'm trying to sum all the values from the July column into a label with ID of july. Here's the markup:

<asp:ListView ID="leaveListView" runat="server" ItemPlaceholderID="itemPlaceholder" OnItemDataBound="leaveListView_ItemDataBound">
    <LayoutTemplate>
        <table class="budgetList">
            <thead>
                <tr>
                    <td>Project Number</td>
                    <td>Account</td>
                    <td>Project Name</td>
                    <td>July</td>
                </tr>
            </thead>
            <tbody>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </tbody>
            <tfoot>
                <tr class="projectRowTotal">
                    <td>Project Totals</td>
                    <td></td>
                    <td></td>
                    <td><asp:Label ID="july" runat="server" Text=""></asp:Label></td>
                </tr>
            </tfoot>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr class="<%# Container.DataItemIndex % 2 == 0 ? "" : "even" %>">
            <td><%# Eval("Project") %></td>
            <td><%# Eval("Account") %></td>
            <td><%# Eval("ProjectNumber") %></td>
            <td><%# Eval("July") %></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

I'm using the ItemDataBound event to sum, but it's returning null. This is the code-behind:

decimal july = 0m;
protected void leaveListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
            july += Convert.ToDecimal(e.Item.FindControl("July"));            
    }
    Label julySum = (Label)leaveListView.FindControl("july");
    julySum.Text = string.Format("{0:C}", (decimal?)july);
}

What do I need to do to get this to work?


Solution

  • as my comments suggested, here is solution:

    <table class="budgetList">
       <thead>
           <tr>
               <td>Project Number</td>
               <td>Account</td>
               <td>Project Name</td>
               <td>July</td>
           </tr>
       </thead>
       <tbody>
       <asp:ListView ID="leaveListView" runat="server" ItemPlaceholderID="itemPlaceholder">
           <LayoutTemplate>
               <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
           </LayoutTemplate>
           <ItemTemplate>
            <tr class="<%# Container.DataItemIndex % 2 == 0 ? "" : "even" %>">
               <td><%# Eval("Project") %></td>
               <td><%# Eval("Account") %></td>
               <td><%# Eval("ProjectNumber") %></td>
               <td><%# Eval("July") %></td>
            </tr>
           </ItemTemplate>
       </asp:ListView>
       </tbody>
            <tfoot>
                <tr class="projectRowTotal">
                    <td>Project Totals</td>
                    <td></td>
                    <td></td>
                    <td><asp:Label ID="july" runat="server" Text=""></asp:Label></td>
                </tr>
            </tfoot>
        </table>
    

    havent't actually tried it, but should work fine ... + in code, where you bind data:

     leaveListView.ItemsSource = <data>;
     // simply place the value in label directly
     july.Text = <data.sum>
    

    now just make sum in code and you're done, no need for itemdatabound event