GridView列合计

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NorstarSaleDetail.aspx.cs"
    Inherits="NorstarSaleDetail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>各分站明细</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width: 100%">
                <tr>
                    <td style="width: 78px">
                        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/hcywfx.gif" /></td>
                    <td align="center" valign="bottom">
                    </td>
                </tr>
                <tr>
                    <td align="right" colspan="2" style="background-image: url(images/headbg.gif); vertical-align: top;
                        height: 16px">
                        &nbsp;</td>
                </tr>
            </table>
        </div>
        <table style="width: 100%">
            <tr>
                <td>
                    <asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="10pt" ForeColor="#400000"></asp:Label><br />
                    <br />
                    <asp:GridView ID="gvInfo" runat="server" Width="98%" AutoGenerateColumns="false" OnRowDataBound="gvInfo_RowDataBound" AllowPaging ="false">
                    <Columns>
                    <asp:BoundField DataField="cCity_Name" HeaderText="城市" />
                     <asp:TemplateField HeaderText="金额">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("iExe", "{0:#,##0.0}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                    </Columns>
                    <HeaderStyle BackColor="#C0C0FF" Font-Size="10pt" />
                        <AlternatingRowStyle BackColor="WhiteSmoke" />
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class NorstarSaleDetail : System.Web.UI.Page
{
    string type = "";
    string cyear = "";
    string cmonth = "";
    string deptname = "";
    //连库字符串
    private string myConnectionString = ConfigurationManager.ConnectionStrings["NetConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            try
            {
                int i = 0;
                type = Request.QueryString["type"].ToString();
                cyear = Request.QueryString["cyear"].ToString();
                cmonth = Request.QueryString["cmonth"].ToString();
                deptname = Request.QueryString["deptname"].ToString();
               
                lblTitle.Text = cyear + "-" + cmonth + "-"+deptname  + "-"+type;
            }
            catch (Exception ex)
            {
                Response.Write("参数不正确!");
                Response.Write(ex.ToString());
                return;
            }

            binddata();
        }
    }
    //绑定GridView
    void binddata()
    {
        string strSql = "";
        string month = "";
        //SQL语句
        switch (type)
        {
            case "发布额":
                if (cmonth == "1-2月")
                {
                    cmonth.Replace("月", "");
                    //Response.Write("2008" + "-" + cmonth.Replace("月", ""));
                    strSql = " SELECT cCity_Name,Sum(iExe) AS iExe FROM( " +
                            " select cCity_Name,iRelease + iAdjust AS iExe  from udc_New3.dbo.tbl_Account_Remain where (cMonth = '2008-01' or cMonth = '2008-02') AND cDeptName = '" + deptname + "' " +
                            " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewSh.dbo.tbl_Account_Remain where (cMonth = '2008-01' or cMonth = '2008-02') AND cDeptName = '" + deptname + "' " +
                            " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewGz.dbo.tbl_Account_Remain where (cMonth = '2008-01' or cMonth = '2008-02') AND cDeptName = '" + deptname + "' " +
                            " ) DD GROUP BY cCity_Name  ORDER BY cCity_Name ";

                }
                else
                    if (cmonth == "03月")
                    {
                        //Response.Write("2008" + "-" + cmonth.Replace("月", ""));
                        strSql = " SELECT cCity_Name,Sum(iExe) AS iExe FROM( " +
                                "  select cCity_Name,iRelease + iAdjust AS iExe  from udc_New3.dbo.tbl_Account_Remain where (cMonth = '2008-03' ) AND cDeptName ='" + deptname + "' " +
                                " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewSh.dbo.tbl_Account_Remain where (cMonth = '2008-03') AND cDeptName = '" + deptname + "' " +
                                " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewGz.dbo.tbl_Account_Remain where (cMonth = '2008-03') AND cDeptName = '" + deptname + "' " +
                                " ) DD GROUP BY cCity_Name ORDER BY cCity_Name ";
                    }
                    else
                    {
                        //Response.Write(cyear + "-" + cmonth.Replace("月", ""));
                        strSql = " SELECT cCity_Name,Sum(iExe) AS iExe FROM( " +
                                "  select cCity_Name,iRelease + iAdjust AS iExe  from udc_New3.dbo.tbl_Account_Remain where (cMonth = '" + cyear + "-" + cmonth.Replace("月", "") + "' ) AND cDeptName = '" + deptname + "' " +
                                " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewSh.dbo.tbl_Account_Remain where (cMonth = '" + cyear + "-" + cmonth.Replace("月", "") + "') AND cDeptName = '" + deptname + "' " +
                                " union ALL select cCity_Name,iRelease + iAdjust AS iExe from udc_NewGz.dbo.tbl_Account_Remain where (cMonth ='" + cyear + "-" + cmonth.Replace("月", "") + "') AND cDeptName = '" + deptname + "' " +
                                " ) DD GROUP BY cCity_Name ORDER BY cCity_Name ";
                    }

                break;
            case "回款额":


                break;
            case "签单额":

                break;
            //default:
            //    break;

        }
        if (strSql == "")
            return;
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlDataAdapter command = new SqlDataAdapter(strSql, myConnection);
        DataSet ds = new DataSet();
        command.Fill(ds, "table");
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count > 0)
        {
            //增加合计行
            DataRow sumRow = dt.NewRow();
            sumRow["cCity_Name"] = " 合计";
            sumRow["iExe"] = 0;
            foreach (DataRow row in dt.Rows)
            {
                sumRow["iExe"] = decimal.Parse(sumRow["iExe"].ToString()) + decimal.Parse(row["iExe"].ToString());
            }
            dt.Rows.Add(sumRow);
        }
        gvInfo.DataSource = dt.DefaultView;
        gvInfo.DataBind();
    }

    protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text.IndexOf("合计") > 0)
            {
                //行颜色
                e.Row.BackColor = System.Drawing.Color.FromName("#C0FFC0");
            }
        }

    }
}


posted @ 2008-06-18 11:05  艾伦  阅读(2709)  评论(0编辑  收藏  举报