首页 > 代码库 > GridView中数据的汇总方法

GridView中数据的汇总方法

  首先,在页面添加事件<ASP:GridView OnRowDataBound="Gridview1_DataBound">

 

  其次,后台具体方法:

 public void Gridview1_DataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (((Label)e.Row.FindControl("IF_VALID")).Text.ToString().Trim() == "作废")
                {
                    e.Row.BackColor = System.Drawing.Color.LightGray;
                }

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string current01 = String.Format("{0:0.00}", DataBinder.Eval(e.Row.DataItem, "COST_NAME"));

                    switch (current01) { case "": total01 += 0; break; default: total01 += Convert.ToDouble(current01); break; }
                }
                

                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[8].Text = "运输成本小计:";
                    e.Row.Cells[9].Text = string.Format("{0:#,##0.00}", total01);

                    Response.Write("<script language =‘javascript‘> window.parent.document.getElementById(‘yscb‘).value="http://www.mamicode.com/+ total01 +";</script> ");

                    Response.Write("<script language =‘javascript‘> window.parent.cal_ct_profit();</script> ");
                }


                string ywcb;
                string yscb;
                string kpfy;
                double profit = 0;

                ywcb = DataBinder.Eval(e.Row.DataItem, "COST_NAME").ToString();
                yscb = DataBinder.Eval(e.Row.DataItem, "COST_YUNSHU").ToString();
                kpfy = DataBinder.Eval(e.Row.DataItem, "Exhibit_amount").ToString();

                switch (ywcb) { case "": profit -= 0; break; default: profit -= double.Parse(ywcb); break; }
                switch (yscb) { case "": profit -= 0; break; default: profit -= double.Parse(yscb); break; }
                switch (kpfy) { case "": profit += 0; break; default: profit += double.Parse(kpfy); break; }

                e.Row.Cells[profit_column].Text = String.Format("{0:0.00}", profit);

                if (profit < 0)
                {
                    e.Row.Cells[profit_column].ForeColor = System.Drawing.Color.Red;
                }

                //string current01 = String.Format("{0:0}", DataBinder.Eval(e.Row.DataItem, "CARTON20_COUNT"));
                //string current02 = String.Format("{0:0}", DataBinder.Eval(e.Row.DataItem, "CARTON40_COUNT"));
                //string current03 = String.Format("{0:0}", DataBinder.Eval(e.Row.DataItem, "CARTON45_COUNT"));
                string current04 = String.Format("{0:0.00}", DataBinder.Eval(e.Row.DataItem, "COST_NAME"));
                string current05 = String.Format("{0:0.00}", DataBinder.Eval(e.Row.DataItem, "COST_YUNSHU"));
                string current06 = String.Format("{0:0.00}", DataBinder.Eval(e.Row.DataItem, "Exhibit_amount"));
                
                //switch (current01) { case "": total01 += 0; break; default: total01 += int.Parse(current01); break; }
                //switch (current02) { case "": total02 += 0; break; default: total02 += int.Parse(current02); break; }
                //switch (current03) { case "": total03 += 0; break; default: total03 += int.Parse(current03); break; }

                switch (current04) { case "": total04 += 0; break; default: total04 += double.Parse(current04); break; }
                switch (current05) { case "": total05 += 0; break; default: total05 += double.Parse(current05); break; }
                switch (current06) { case "": total06 += 0; break; default: total06 += double.Parse(current06); break; }
                //total07 += profit;
            }

            total00 = GridView1.Rows.Count;

            ShowPageStatus(string.Format("{0:#,##0}", total00), total04, total05, total06);

        }

        private void ShowPageStatus(string nRecords, double ywcb, double yscb, double kpfy)
        {
            ShowTTL.Text = "总计<b><FONT color= #9C0001> " + nRecords + " </FONT></b>箱,  出车费<b><FONT color= #9C0001> " + ywcb + " </FONT></b>元,  运输费<b><FONT color= #9C0001> " + yscb + " </FONT></b>元,  亏盈金额<b><FONT color= #9C0001> " + kpfy + " </FONT></b>元";
        }


        private void ShowCTTTL(double T20, double T40, double T45)
        {
            ShowCount.Text = "20尺 <b><FONT color= #9C0001> " + T20 + " </FONT></b>, 40尺 <b><FONT color= #9C0001> " + T40 + " </FONT></b>, 45尺 <b><FONT color= #9C0001> " + T45 + " </FONT></b>。";
        }

GridView中数据的汇总方法