gridview页脚中的列总和 [英] Sum of a Column in gridview Footer

查看:77
本文介绍了gridview页脚中的列总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在这里粘贴我的代码,我想在模板的页脚中加上总时数和总分钟数,并提供在aspx.page
中所做的事情 如果我在某个地方输入错误,请告诉我,我想使用计算方法对页脚中的列求和.

我的aspx代码为

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

<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

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

推荐答案

如果网格没有良好的求和函数,我们通常会这样做(我不记得GridView是否具有或不是),是将单元格设置为itemtemplate,然后在代码隐藏区中调用方法以将值存储在字典中,然后再次返回该值.在页脚的后面,调用一个方法以返回该列的总值.

在此处编辑一些c#代码背后的示例:

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:

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() %>



您可以总计任意多的列,而无需为每个列创建单独的公共/全局变量.只需将唯一的列名传递给saveValue方法.当网格不支持开箱即用时,我们经常使用它来总计货币列.

HTH -rog



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


Mohd Wasif,

声明全局变量TotalHours,TotalMinutes,

尝试使用以下事件.
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

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



同样适用于TotalMinutes的方法.



same way applicable to TotalMinutes also.


这篇关于gridview页脚中的列总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆