在gridview的最后一页结尾处的Grandtotal

时间:2017-04-19 18:50:55

标签: c# asp.net

我想计算gridview最后一页末尾的总计。现在,我在每页末尾都得到了总数。我不希望在每个页面的末尾有总计,我希望在最后一页的最后总计。这是我到目前为止计算每页末尾的总数。

private void BindGrid()
{
    DataSet ds = new Projects.getdataa();
    if (ds.Tables.Count > 0)
    {
        grdDateReport.DataSource = ds;
        columnCount = ds.Tables[0].Columns.Count;

        grdDateReport.DataBind();
        CalculateTotalHours();
    }
}

我的gridview看起来像这样:

<asp:GridView ID="grdDateReport" OnRowDataBound="grdDateReport_RowDataBound" AutoGenerateColumns="true" runat="Server" AllowPaging="true" CssClass="GridView1"
    Font-Names="Arial" PageSize="25" Width="90%" BorderColor="#CCCCCC" BorderStyle="Solid" ShowFooter="true"
    BorderWidth="1px" OnPageIndexChanging="grdDateReport_PageIndexChanging" Height="100px" Font-Size="10px" OnDataBound="grdDateReport_DataBound">
    <AlternatingRowStyle BackColor="#BFE4FF" />
    <HeaderStyle Height="20px" BackColor="#6DC2FF" Font-Size="10px" BorderColor="#CCCCCC"
        BorderStyle="Solid" BorderWidth="0px" />
    <RowStyle Height="2px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
        BorderWidth="1px" />
    <EmptyDataTemplate>No Data for the above selections</EmptyDataTemplate>
</asp:GridView>

我的CalculateTotalHours看起来像这样

private void CalculateTotalHours()
{
    for (int i = 1; i < columnCount - 1; i++)
    {
        double total = 0;
        foreach (GridViewRow row in grdDateReport.Rows)
        {
            if (row.Cells[i].Text != "&nbsp;")
            {
                total += Convert.ToDouble(row.Cells[i].Text);
            }
        }
        grdDateReport.FooterRow.Cells[i].Text = total.ToString();
    }
}

我的分页就像这样

protected void grdDateReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdDateReport.PageIndex = e.NewPageIndex;
    BindGrid();
}

我的Gridview看起来像这样

Projects       Test1        Test2       Test3      Test4

 Tickets           8           10         0          0

 maintenance       9          11         13        0


 Writing web       8            9          8        0

  VSS                                                 12.5

我需要将总数放在Test1,Test2和Test3的最后一页上。列号可以更改。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据总页数检查NewPageIndex并在页脚中显示总数。

protected void grdDateReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdDateReport.PageIndex = e.NewPageIndex;
    BindGrid();

    if (grdDateReport.PageCount == e.NewPageIndex + 1)
    {
        grdDateReport.FooterRow.Cells[0].Text = string.Format("{0:N2}", total);
    }
}