我可以使用c#添加gridview的百分比和小计吗?

时间:2014-12-19 09:15:44

标签: c# sql gridview

我有一个从SQL语句派生的gridview。我现在想要显示每列的小计,然后显示2个日期范围之间的总增加/减少百分比。我该怎么做呢?我想用C#而不是SQL语句来做这件事。

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Sales-Comparison.aspx.cs" Inherits="TowelsRusV3.Sales_Comparison" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <div class="panel panel-info">
      <div class="panel-heading">
          <h1 class="panel-title text-center">Sales Comparison</h1>

      </div>
      <div class="panel-body">
       <div class="row">
           <div class="col-lg-8">
                         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CssClass="table table-bordered table-responsive table-striped" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowFooter="True">
                  <Columns>
                      <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" ReadOnly="True" SortExpression="OrderDate" />
                      <asp:BoundField DataField="Total2014" HeaderText="Total2014" ReadOnly="True" SortExpression="Total2014" />
                      <asp:BoundField DataField="Total2013" HeaderText="Total2013" ReadOnly="True" SortExpression="Total2013" />
                      <asp:BoundField DataField="Difference" HeaderText="Difference" ReadOnly="True" SortExpression="Difference" />
                  </Columns>
              </asp:GridView>
              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LiveDataV2ConnectionString %>" SelectCommand="SELECT 
    Table2014.OrderDate,
    isnull(Table2014.Total,0) as Total2014,
    isnull(Table2013.Total,0) as Total2013,
    isnull(Table2014.Total,0)-isnull(Table2013.Total,0) as Difference

    FROM 
           (SELECT SUM(order_header_total.oht_net) AS Total,
           dateadd(day,0,DATEDIFF(d,0,order_header.oh_datetime)) as OrderDate
           FROM order_header_total
           INNER JOIN order_header ON order_header_total.oht_oh_id = order_header.oh_id
           WHERE (order_header.oh_datetime BETWEEN '12/01/2014' AND '12/31/2014') 
           AND order_header.oh_os_id IN (1, 6, 4)
           GROUP BY dateadd(day,0,DATEDIFF(d,0,order_header.oh_datetime))) Table2014

    LEFT OUTER JOIN
           (SELECT SUM(order_header_total.oht_net) AS Total,
           dateadd(year,1,dateadd(day,0,DATEDIFF(d,0,order_header.oh_datetime))) as OrderDate
           FROM order_header_total
           INNER JOIN order_header ON order_header_total.oht_oh_id = order_header.oh_id
           WHERE (order_header.oh_datetime BETWEEN dateadd(year,-1,'12/01/2014') AND dateadd(year,-1,'12/31/2014'))
           AND order_header.oh_os_id IN (1, 6, 4)
           GROUP BY dateadd(year,1,dateadd(day,0,DATEDIFF(d,0,order_header.oh_datetime)))) as Table2013 On Table2013.OrderDate = Table2014.OrderDate

    order by Table2014.OrderDate"></asp:SqlDataSource>
               </div>
           </div>
          </div>
            </div>
    </asp:Content>

1 个答案:

答案 0 :(得分:0)

您可以在RowDataBound事件中执行此操作:

decimal total2014  = 0;
decimal total2015  = 0;
decimal difference = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{      
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        total2014  += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total2014"));
        total2015  += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total2015"));
        difference += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Difference"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        GridView1.FooterRow.Cells[1].Text = String.Format("{0:c}", total2014);
        GridView1.FooterRow.Cells[2].Text = String.Format("{0:c}", total2015);
        GridView1.FooterRow.Cells[3].Text = String.Format("{0:c}", difference);
    }
}

.aspx文件中添加OnRowDataBound属性,如下所示:

<asp:GridView ID="GridView1" runat="server" ... OnRowDataBound="GridView1_RowDataBound" ...>