如何在Gridview上的小计下面的行中计算百分比?

时间:2014-12-22 13:01:37

标签: c# sql asp.net gridview

我想在GridView上创建更多行,或者它们可以是独立的单元格,显示December2014DailySalesDecember2013DailySales小计之间的百分比变化。我提供了代码。我该怎么做?

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;

        namespace TowelsRusV3
        {
            public partial class Sales_Comparison : System.Web.UI.Page
            {
                protected void Page_Load(object sender, EventArgs e)
                {

                }
                private decimal December2014DailySales = (decimal)0.0;
                private decimal December2013DailySales = (decimal)0.0;
                private decimal DailySalesDifference = (decimal)0.0;
                private decimal December2014DailyOrderCount = (decimal)0.0;
                private decimal December2013DailyOrderCount = (decimal)0.0;
                private decimal DailyOrderCountDifference = (decimal)0.0;
                protected void salecompgridview_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        December2014DailySales += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "December2014DailySales"));
                        December2013DailySales += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "December2013DailySales"));
                        DailySalesDifference += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "DailySalesDifference"));
                        December2014DailyOrderCount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "December2014DailyOrderCount"));
                        December2013DailyOrderCount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "December2013DailyOrderCount"));
                        DailyOrderCountDifference += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "DailyOrderCountDifference"));

                    }

                    else if (e.Row.RowType == DataControlRowType.Footer)
                    {
                        e.Row.Cells[1].Text = String.Format("{0:c}", December2014DailySales);
                        e.Row.Cells[3].Text = String.Format("{0:c}", December2013DailySales);
                        e.Row.Cells[5].Text = String.Format("{0:c}", DailySalesDifference);
                        e.Row.Cells[2].Text = String.Format("{0:g}", December2014DailyOrderCount);
                        e.Row.Cells[4].Text = String.Format("{0:g}", December2013DailyOrderCount);
                        e.Row.Cells[6].Text = String.Format("{0:g}", DailyOrderCountDifference);
                    }

                }
            }
        }

2 个答案:

答案 0 :(得分:0)

尝试手动添加行,并将空单元格设置为“”,将非空单元格设置为预先计算的值。希望它有所帮助。

答案 1 :(得分:0)

您可以将该值存储在网格的任何单元格中,如下所示:

salecompgridview.Rows[0].Cells[0].Text = ((December2014DailySales - December2013DailySales) / December2013DailySales * 100).ToString();

只需使用适当的行和单元格索引。