我使用 c# 添加网格视图的百分比和小计

本文关键字:百分比 视图 网格 添加 | 更新日期: 2023-09-27 18:34:44

我有一个从SQL语句派生的网格视图。我现在想显示每列的小计,然后显示 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>

我使用 c# 添加网格视图的百分比和小计

您可以在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" ...>