Click here to Skip to main content
15,943,623 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,
I am pasting my code here I want to sum of total hours and total minutes in footer of template and i am providing what I have done in aspx.page
If I'm wrong somewhere please let me know and I want to sum of column in footer using compute method.

my aspx code as
XML
<asp:GridView  ID="GvMonthlyAttendance" Width="100%" runat="server" CellPadding="4" ForeColor="#000099"
                    GridLines="None" AutoGenerateColumns="False" Font-Names="Verdana"
                    Font-Size="Small">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:BoundField DataField="Date" HeaderText="Date">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="DayOW" HeaderText="Day" />
                        <asp:BoundField DataField="LoginT" HeaderText="Login Time">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="LogouT" HeaderText="Logout Time">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="Hours">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TotalHours") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                            <asp:Label ID="lblTotalHours" runat="server" ></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("TotalHours") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Minutes">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("TotalMinutes") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                            <asp:Label ID="lblTotalMinutes" runat="server" ></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("TotalMinutes") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <EmptyDataTemplate>
                    !!!!! No Record are Found !!!!!
                    </EmptyDataTemplate>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                </asp:GridView>





and my code behind code is as
below

C#
if (dt.Rows.Count > 0)
            {
                GvMonthlyAttendance.DataSource = dt;
                GvMonthlyAttendance.DataBind();
                ((Label)GvMonthlyAttendance.FooterRow.Cells[4].FindControl("lblTotalHours")).Text = dt.Compute("sum(TotalHours)", "").ToString();
                ((Label)GvMonthlyAttendance.FooterRow.Cells[5].FindControl("lblTotalMinutes")).Text = dt.Compute("sum(lblTotalMinutes)", "").ToString();

}


Question is Solved
Posted
Updated 2-Dec-10 22:52pm
v3
Comments
Mohd Wasif 3-Dec-10 4:46am    
Question is Solved

The way we usually do it, if the grid doesn't have a good sum function (I can't remember if GridView does or not), is to set the cell to an itemtemplate then call a method in the codebehind to stash the value in a dictionary and return the value again. Later in the footer, call a method to return the total value for the column.

Edit here's an example of some c# codebehind code:

C#
public Dictionary<string, decimal> columnTotals;

public decimal saveValue(string colName, decimal cellValue)
{
    columnTotals(colName) += cellValue;
    return cellValue;
}

public decimal getColumnTotal(string colName)
{
    return columnTotals(colName);
}


Then inside your grid call it via:

Data row item template code:

Hours cell: <%# saveValue("totalHours", Eval("TotalHours")).ToString() %>

Mins cell: <%# saveValue("totalMinutes", Eval("Minutes")).ToString() %>


Footer row item template code:

Hours cell: <%# ((int)getColumnTotal("totalHours")).ToString() %>

Mins cell: <%# ((int)getColumnTotal("totalMinutes")).ToString() %>


You can total as many columns as you like without needing to create separate public/global variables for each. Simply pass in unique column names to the saveValue method. We use it for totaling money columns frequently when the grid doesn't support it out of the box.

HTH -rog
 
Share this answer
 
v3
Hi Mohd Wasif,

Declare global variables TotalHours, TotalMinutes,

try use following event.
GvMonthlyAttendance_RowDataBound


use this condition for sum of all rows.

if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblHours= (Label)e.Row.FindControl("lblTotalHours");
            if (lblHours!= null)
            {
                if (lblHours.Text != "")
                    TotalHours+= Convert.ToDouble(lblHours.Text);

}


condition for display in footer

C#
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text ="Total" +  TotalHours.ToString();
}


same way applicable to TotalMinutes also.
 
Share this answer
 
Comments
Mohd Wasif 1-Dec-10 3:47am    
Thanks for ur suggestion .i sorted out my problem here show footer visibility was false i did it true right now its running well
TweakBird 1-Dec-10 4:05am    
please update question as 'Solved'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900