900行代码【原创】期待点评

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

public partial class Caiwu_Bills_JiXiaoHeCha : System.Web.UI.Page
{
   
    public myLib lib = new myLib();
    myLib.wangtao wt = new myLib.wangtao();
    //执行人
    int tiaoshu=1;
    string firstAccountname = string.Empty;
    string firstCompany = string.Empty;
    static string operatePerson ="";
    static int tempyue;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        operatePerson = Session["userid"].ToString();
        lib.init_my_session(this, Convert.ToInt16(Session["userid"]), Request.Cookies["superrealm"]);
        if (!IsPostBack)
        {
            JiXiaoBind(DropDownList3);
            JiXiaoBind(DropDownList4);
            JiXiaoBind(DropDownList5);
            Master.label1Text = lib.myfun.loadusername(this);
            myLib.SrUSERS us = new myLib.SrUSERS();
            if (!us.isCLS(lib.userid) && (DateTime.Now.Hour >= 19 || DateTime.Now.Hour <= 7))
                this.Response.Redirect("/");
            GetDataBind(Session["userid"].ToString());
           
           
        }
       
    }
   
    /// <summary>
    /// 传入UserID 返回ID,名称
    /// </summary>
    /// <param name="userid">用户ID</param>
    /// <returns>DataTable</returns>
    public DataTable GetDsById(string userid)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        string sql;
        bool flag = IsLeader(userid);
        if (flag)
        {
            sql = "select userid,username from clipping.dbo.users where department in(select department from clipping.dbo.users where userid='" + userid + "') and  expired=0";
           
        }
        else
        {
            sql = "select userid,username from clipping.dbo.users where userid='"+userid+"'";
        }
        da = new SqlDataAdapter(sql, wt.GetConnection());
        da.Fill(ds);
        return ds.Tables[0];
       
    }

    /// <summary>
    /// 根据用户ID 判断是否为Leader
    /// </summary>
    /// <param name="userid">用户ID</param>
    /// <returns>bool</returns>
    public bool IsLeader(string userid)
    {
        bool flag = false;
        string sql = "select * from finance.dbo.sa_department where Leader='"+userid+"'";
        using (SqlCommand cmd = new SqlCommand(sql, wt.GetConnection()))
        {
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    flag=true;
                }
            }
        }
        return flag;
       
    }

    /// <summary>
    /// 绑定人员信息
    /// </summary>
    /// <param name="userid">用户ID</param>
    public void GetDataBind(string userid)
    {
        rblPerson.DataSource = GetDsById(userid);
        rblPerson.DataValueField = "userid";
        rblPerson.DataTextField = "username";
        rblPerson.DataBind();
    }

    /// <summary>
    /// RadionButtonList事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rblPerson_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        ReLoad();
        //Differencestreatment();
    }
   
    /// <summary>
    /// 根据用户ID 查出companyid 并以此加载每个companyid账单信息的前十条.并且每个账单信息后动态创建一个DataRow
    /// </summary>
    /// <param name="userid">用户ID</param>
    /// <returns>DataSet</returns>
    public DataSet GetDataSet(string userid)
    {
       SqlDataAdapter sd;
       DataSet ds =new DataSet();

        string sql = "select distinct companyid from finance.dbo.Bill where companyid in (select companyid from finance.dbo.Company where XiangmuUserid='" + userid + "')";
        int i = 1;
        using (SqlCommand cmd = new SqlCommand(sql, wt.GetConnection()))
        {
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    string companyid = reader.GetInt16(0).ToString();
                    string sqlwhile = "select top 10 billID,accountid,companyid,account_name,company,total,yymm,CashDate,zhangdanDate, flag,isoperate from finance.dbo.View_Bill where companyid='" + companyid + "'  order by yymm desc";
                    if (IsCount(sqlwhile))
                    {
                        sd = new SqlDataAdapter(sqlwhile, wt.GetConnection());
                        sd.Fill(ds);
                        firstAccountname = ds.Tables[0].Rows[0][3].ToString();
                        firstCompany = ds.Tables[0].Rows[0][4].ToString();
                        DataRow dr = ds.Tables[0].NewRow();
                        dr[6] = "1900-1-1";
                        ds.Tables[0].Rows.Add(dr);
                        tiaoshu++;
                    }
                    i++;
                }
            }
               
        }
        return ds;
    }


    /// <summary>
    /// 根据用户ID绑定GridView 只对非空数据源操作
    /// </summary>
    /// <param name="userid">用户ID</param>
    public void GridViewBind(string userid)
    {
        DataSet ds = GetDataSet(userid);
        if (ds.Tables.Count!=0)
        {
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            GridViewRowColor(this.GridView1);
            //隐藏最后一行
            this.GridView1.Rows[GridView1.Rows.Count-1].Visible = false;
           
        }
       
    }

   
    /// <summary>
    /// 循环遍历GridView 对于空行红色显示 并隐藏空行的CheckBox和Button按钮 对于已勾选行粉色显示
    /// </summary>
    /// <param name="gv">GridView</param>
    public void GridViewRowColor(GridView gv)
    {
        int cishu = 1;
        for (int i = 0; i < gv.Rows.Count; i++)
        {
           
            if (gv.Rows[i].Cells[6].Text.Contains("1900"))
            {
                gv.Rows[i].BackColor = System.Drawing.Color.Blue;
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                cb.Visible = false;
                LinkButton btn = (LinkButton)GridView1.Rows[i].FindControl("btnReset");
                btn.Visible = false;
                LinkButton btn1 = (LinkButton)GridView1.Rows[i].FindControl("btnedit");
                btn1.Visible = false;
               
                if (cishu<tiaoshu-1)
                {
                    CreatedRow(GridView1, i, this.GridView1.DataKeys[i + 1]["account_name"].ToString(), gv.DataKeys[i + 1]["company"].ToString());
                    cishu++;
                }
            }
            if (this.GridView1.DataKeys[i]["flag"].ToString() == "1")
            {
                CheckBox cb = (CheckBox)gv.Rows[i].FindControl("CheckBox1");
                cb.Enabled = false;
                LinkButton btn1 = (LinkButton)gv.Rows[i].FindControl("btnedit");
                btn1.Enabled = false;
                gv.Rows[i].BackColor = System.Drawing.Color.FromName("#F3ECD9");
            }

            if (this.GridView1.DataKeys[i]["isoperate"].ToString() == "1")
            {
                LinkButton btn = (LinkButton)GridView1.Rows[i].FindControl("btnReset");
                btn.Enabled = false;
            }

        }
    }

    /// <summary>
    /// 创建新的标题
    /// </summary>
    /// <param name="gv">GridView1</param>
    /// <param name="rowindex">当前行index</param>
    public void CreatedRow(GridView gv,int rowindex, string acc,string com)
    {
        //获取表头所在行的所有单元格
        TableCellCollection tcHeader = gv.Rows[rowindex].Cells;
        //清除自动生成的表头
        tcHeader.Clear();

        //新添加的第一个表头单元格, 设置为合并7个列, 从而形成一行.
        tcHeader.Add(new TableHeaderCell());
        tcHeader[0].ColumnSpan = 7;
        tcHeader[0].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[0].Text = acc+"</th></tr><tr>";
        tcHeader[0].BackColor = System.Drawing.Color.FromName("#9E4120");
        tcHeader[0].ForeColor = System.Drawing.Color.White;
        //</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行    关键点

        //添加第二个表头单元格, 设置为合并两行.
        tcHeader.Add(new TableHeaderCell());
        tcHeader[1].ColumnSpan = 4;
        tcHeader[1].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[1].Text = com;
        tcHeader[1].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[1].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[2].ColumnSpan = 3;
        tcHeader[2].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[2].Text = " </th></tr><tr>";
        tcHeader[2].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[2].ForeColor = System.Drawing.Color.White;


        //第二行的所有的单元格添加完成, 换行</th></tr><tr>

        //添加第三行所有的单元格
        tcHeader.Add(new TableHeaderCell());
        tcHeader[3].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[3].Text = "选择";
        tcHeader[3].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[3].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[4].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[4].Text = "月份";
        tcHeader[4].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[4].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[5].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[5].Text = "金额";
        tcHeader[5].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[5].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[6].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[6].Text = "账单日期";
        tcHeader[6].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[6].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[7].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[7].Text = "到账日期";
        tcHeader[7].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[7].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[8].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[8].Text = "编辑";
        tcHeader[8].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[8].ForeColor = System.Drawing.Color.White;

        tcHeader.Add(new TableHeaderCell());
        tcHeader[9].HorizontalAlign = HorizontalAlign.Center;
        tcHeader[9].Text = "重置";
        tcHeader[9].BackColor = System.Drawing.Color.FromName("#676566");
        tcHeader[9].ForeColor = System.Drawing.Color.White;
    }
    /// <summary>
    /// 根据用户ID 判断有多少个不同的companyid
    /// </summary>
    /// <param name="userid">用户ID</param>
    /// <returns>int</returns>
    public int ReturnCount(string userid)
    {
        int i = 0;
        string sql = "select count(distinct companyid) from finance.dbo.Bill where companyid in (select companyid from finance.dbo.Company where XiangmuUserid='"+userid+"')";
        using (SqlCommand cmd = new SqlCommand(sql, wt.GetConnection()))
        {
            i = Convert.ToInt32(cmd.ExecuteScalar());
        }
        return i;
    }

    /// <summary>
    /// 查询SQL语句结果集是否为空
    /// </summary>
    /// <param name="sql">SQL语句</param>
    /// <returns></returns>
    public bool IsCount(string sql)
    {
        bool flag = false;
        using (SqlCommand cmd = new SqlCommand(sql, wt.GetConnection()))
        {
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    flag = true;
                }
            }
        }
        return flag;
    }

    /// <summary>
    /// 遍历循环 将选中的记录行插入数据库中,并重新加载数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btntj_Click(object sender, EventArgs e)
    {
        string str=DropDownList3.SelectedItem.Text;
        if (DropDownList3.SelectedItem.Value == "-")
        {
            return;
        }

        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");

            if (cb.Checked == true)
            {
                try
                {
                    //GridView_UpdateData.DataKeys[e.Row.DataItemIndex]["stateid_O2"].ToString() == "1")
                    InsertBySelect(Convert.ToInt32(this.GridView1.DataKeys[i]["billID"].ToString()),
                        Convert.ToInt32(rblPerson.SelectedItem.Value),
                        Convert.ToInt32(this.GridView1.DataKeys[i]["accountid"].ToString()),
                        Convert.ToInt16(this.GridView1.DataKeys[i]["companyid"].ToString()),
                        this.GridView1.Rows[i].Cells[6].Text.ToString() + "-01",
                        Convert.ToDecimal(this.GridView1.Rows[i].Cells[7].Text),
                        this.GridView1.Rows[i].Cells[9].Text.ToString(),
                        this.GridView1.Rows[i].Cells[8].Text.ToString(),
                        this.DropDownList3.SelectedItem.Text.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            
 

        }

        ReLoad();

    }
   
    /// <summary>
    /// 重置 编辑 分条 取消 按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //重置
        if (e.CommandName == "gvReset")
        {
            string billid = e.CommandArgument.ToString();
            string sql = "delete from finance.dbo.JiXiao_ContractAmount where billid='" + billid + "'";
            SqlCommand cmd = new SqlCommand(sql, wt.GetConnection());
            cmd.ExecuteNonQuery();
        }
        //编辑
        if (e.CommandName == "gvEdit")
        {
            GridViewRow dvr = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
            GridView1.EditIndex = dvr.RowIndex;
        }

        //分条
        if (e.CommandName == "gvft")
        {
            GridViewRow dvr = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            DropDownList ddl1=(DropDownList)dvr.FindControl("DropDownList1");
            TextBox tb1 = (TextBox)dvr.FindControl("TextBox1");
            DropDownList ddl2 = (DropDownList)dvr.FindControl("DropDownList2");
            TextBox tb2 = (TextBox)dvr.FindControl("TextBox2");

            //限制分条相加金额等于总金额
            Decimal ztotal = Convert.ToDecimal(((TextBox)GridView1.Rows[dvr.RowIndex].Cells[7].Controls[0]).Text.ToString());
            if (ztotal != (Convert.ToDecimal(tb1.Text) + Convert.ToDecimal(tb2.Text)))
            {
                Response.Write("<script>alert('删除成功!')</script>");
                return;
            }

            InsertBySelect(Convert.ToInt32(this.GridView1.DataKeys[dvr.RowIndex]["billID"].ToString()),
                      Convert.ToInt32(ddl1.SelectedItem.Value),
                      Convert.ToInt32(this.GridView1.DataKeys[dvr.RowIndex]["accountid"].ToString()),
                      Convert.ToInt16(this.GridView1.DataKeys[dvr.RowIndex]["companyid"].ToString()),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[6].Controls[0]).Text.ToString() ,
                      Convert.ToDecimal(tb1.Text),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[9].Controls[0]).Text.ToString(),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[8].Controls[0]).Text.ToString(),
                      this.DropDownList3.SelectedItem.Text.ToString());
           

           

           


            InsertBySelect(Convert.ToInt32(this.GridView1.DataKeys[dvr.RowIndex]["billID"].ToString()),
                      Convert.ToInt32(ddl2.SelectedItem.Value),
                      Convert.ToInt32(this.GridView1.DataKeys[dvr.RowIndex]["accountid"].ToString()),
                      Convert.ToInt16(this.GridView1.DataKeys[dvr.RowIndex]["companyid"].ToString()),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[6].Controls[0]).Text.ToString(),
                      Convert.ToDecimal(tb2.Text),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[9].Controls[0]).Text.ToString(),
                      ((TextBox)GridView1.Rows[dvr.RowIndex].Cells[8].Controls[0]).Text.ToString(),
                      this.DropDownList3.SelectedItem.Text.ToString());

                      GridView1.EditIndex = -1;
        }

        //取消
        if (e.CommandName == "gvqx")
        {
            GridView1.EditIndex = -1;
        }

        //重新加载
        ReLoad();
    }


    /// <summary>
    /// 重新加载人员信息
    /// </summary>
    public void ReLoad()
    {
        try
        {
            string str = rblPerson.SelectedItem.Value;
            //string userid = Convert.ToString(rblPerson.SelectedItem.Text);
            GridViewBind(str);
        }
        catch (Exception ex)
        {
           
        }
    }

    /// <summary>
    /// 插入数据库表JiXiao_ContractAmount
    /// </summary>
    /// <param name="billid"></param>
    /// <param name="accountid"></param>
    /// <param name="companyid"></param>
    /// <param name="yymm"></param>
    /// <param name="total"></param>
    /// <param name="cashDate"></param>
    /// <param name="zhangdanDate"></param>
    public void InsertBySelect(int billid, int userid, int accountid, short companyid, string yymm, decimal total, string cashDate, string zhangdanDate, string jiXiaoDate)
    {
        int isop = 0;
        string sql = " insert into finance.dbo.JiXiao_ContractAmount(billid,userid,accountid,companyid,yymm,total,CashDate,ZhangdanDate,operateDate,operatePerson,isoperate,JiXiaoDate)values(@billid,@userid,@accountid,@companyid,@yymm,@total,(select case when @CashDate='&nbsp;' then null else @CashDate end),(select case when @ZhangdanDate='&nbsp;' then null else @ZhangdanDate end),@operateDate,@operatePerson,@isoperate,@jiXiaoDate)";
        using (SqlCommand cmd = new SqlCommand(sql, wt.GetConnection()))
        {
            cmd.Parameters.Add(new SqlParameter("@billid", billid));
            cmd.Parameters.Add(new SqlParameter("@userid", userid));
            cmd.Parameters.Add(new SqlParameter("@accountid", accountid));
            cmd.Parameters.Add(new SqlParameter("@companyid", companyid));
            cmd.Parameters.Add(new SqlParameter("@yymm", yymm));
            cmd.Parameters.Add(new SqlParameter("@total", total));
            cmd.Parameters.Add(new SqlParameter("@CashDate", cashDate));
            cmd.Parameters.Add(new SqlParameter("@ZhangdanDate", zhangdanDate));
            cmd.Parameters.Add(new SqlParameter("@operateDate",DateTime.Now.ToShortDateString().ToString()));
            cmd.Parameters.Add(new SqlParameter("@operatePerson",Convert.ToInt32(operatePerson)));
            cmd.Parameters.Add(new SqlParameter("@isoperate", isop));
            cmd.Parameters.Add(new SqlParameter("@jiXiaoDate",jiXiaoDate+"-01"));
            cmd.ExecuteNonQuery();
        }
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //编辑时控件禁用
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Enabled = false;
            e.Row.Cells[7].Enabled = false;
            e.Row.Cells[8].Enabled = false;
            e.Row.Cells[9].Enabled = false;
            //e.Row.Cells[7].Enabled = false;
            //e.Row.Cells[8].Enabled = false;
        }
    }

    /// <summary>
    /// 创建标题
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //获取表头所在行的所有单元格
            TableCellCollection tcHeader = e.Row.Cells;
            //清除自动生成的表头
            tcHeader.Clear();

            //新添加的第一个表头单元格, 设置为合并7个列, 从而形成一行.
            tcHeader.Add(new TableHeaderCell());
            tcHeader[0].ColumnSpan = 7;
            tcHeader[0].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[0].Text = firstAccountname+"</th></tr><tr>";
            tcHeader[0].BackColor = System.Drawing.Color.FromName("#9E4120");
            tcHeader[0].ForeColor = System.Drawing.Color.White;
            //</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行    关键点

            //添加第二个表头单元格, 设置为合并两行.
            tcHeader.Add(new TableHeaderCell());
            tcHeader[1].ColumnSpan = 4;
            tcHeader[1].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[1].Text =firstCompany;
            tcHeader[1].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[1].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[2].ColumnSpan = 3;
            tcHeader[2].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[2].Text = "</th></tr><tr>";
            tcHeader[2].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[2].ForeColor = System.Drawing.Color.White;


            //第二行的所有的单元格添加完成, 换行</th></tr><tr>

            //添加第三行所有的单元格
            tcHeader.Add(new TableHeaderCell());
            tcHeader[3].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[3].Text = "选择";
            tcHeader[3].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[3].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[4].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[4].Text = "月份";
            tcHeader[4].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[4].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[5].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[5].Text = "金额";
            tcHeader[5].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[5].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[6].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[6].Text = "账单日期";
            tcHeader[6].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[6].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[7].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[7].Text = "到账日期";
            tcHeader[7].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[7].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[8].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[8].Text = "编辑";
            tcHeader[8].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[8].ForeColor = System.Drawing.Color.White;

            tcHeader.Add(new TableHeaderCell());
            tcHeader[9].HorizontalAlign = HorizontalAlign.Center;
            tcHeader[9].Text = "重置";
            tcHeader[9].BackColor = System.Drawing.Color.FromName("#676566");
            tcHeader[9].ForeColor = System.Drawing.Color.White;
        }


    }

    /// <summary>
    /// 对金额前后又变化的进行处理
    /// </summary>
    public void Differencestreatment()
    {
        string sql1 = "select a.billid,a.total as a_total,b.total as b_total,(a.total-b.total) as cha from finance.dbo.View_Bill as a inner join (select billid,sum(total) as total from finance.dbo.JiXiao_ContractAmount group by finance.dbo.JiXiao_ContractAmount.billid) as b on a.billid=b.billID and a.total<>b.total";
        string sql2 = string.Empty;
        int billid = 0;
        Decimal chazhi =0;
        using (SqlCommand cmd1 = new SqlCommand(sql1, wt.GetConnection()))
        {
            using (SqlDataReader reader1 = cmd1.ExecuteReader())
            {
                while (reader1.Read())
                {
                    billid=Convert.ToInt32(reader1.GetInt32(0).ToString());
                    chazhi = Convert.ToDecimal(reader1.GetDecimal(3).ToString());
                    sql2 = "select * from finance.dbo.JiXiao_ContractAmount where billid=" + billid + "";

                    using (SqlCommand cmd2 = new SqlCommand(sql2, wt.GetConnection()))
                    {
                        using (SqlDataReader reader2 = cmd2.ExecuteReader())
                        {
                            if (reader2.Read())
                            {
                                InsertBySelect(billid,
                                Convert.ToInt32(rblPerson.SelectedItem.Value),
                                Convert.ToInt32(reader2.GetInt32(1).ToString()),
                                Convert.ToInt16(reader2.GetInt16(3).ToString()),
                                reader2.GetDateTime(4).ToString(),
                                chazhi,
                                reader2.GetDateTime(6).ToString(),
                                reader2.GetDateTime(7).ToString(),
                                this.DropDownList3.SelectedItem.Text.ToString());
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 对金额前后又变化的进行处理
    /// </summary>
    public void Differencestreatment(string bill_id)
    {
        string sql1 = "select a.billid,a.total as a_total,b.total as b_total,(a.total-b.total) as cha from finance.dbo.View_Bill as a inner join (select billid,sum(total) as total from finance.dbo.JiXiao_ContractAmount group by finance.dbo.JiXiao_ContractAmount.billid) as b on a.billid=b.billID and a.total<>b.total where a.billid='"+bill_id+"'";
        string sql2 = string.Empty;
        int billid = 0;
        Decimal chazhi = 0;
        using (SqlCommand cmd1 = new SqlCommand(sql1, wt.GetConnection()))
        {
            using (SqlDataReader reader1 = cmd1.ExecuteReader())
            {
                while (reader1.Read())
                {
                    billid = Convert.ToInt32(reader1.GetInt32(0).ToString());
                    chazhi = Convert.ToDecimal(reader1.GetDecimal(3).ToString());
                    sql2 = "select * from finance.dbo.JiXiao_ContractAmount where billid=" + billid + "";

                    using (SqlCommand cmd2 = new SqlCommand(sql2, wt.GetConnection()))
                    {
                        using (SqlDataReader reader2 = cmd2.ExecuteReader())
                        {
                            if (reader2.Read())
                            {
                                InsertBySelect(billid,
                                Convert.ToInt32(reader2.GetInt32(2).ToString()),
                                Convert.ToInt32(reader2.GetInt32(1).ToString()),
                                Convert.ToInt16(reader2.GetInt16(3).ToString()),
                                reader2.GetDateTime(4).ToString(),
                                chazhi,
                                reader2.GetDateTime(6).ToString(),
                                reader2.GetDateTime(7).ToString(),
                                this.DropDownList4.SelectedItem.Text.ToString());
                            }
                        }
                    }
                }
            }
        }
    }

    //绑定绩效月份
    public void JiXiaoBind(DropDownList ddlt)
    {
        tempyue = 0;
        ddlt.Items.Clear();
        string datenow = DateTime.Now.ToShortDateString().ToString().Substring(0,5);
        int yue=Convert.ToInt32(DateTime.Now.Month.ToString())-3;
      
        //DropDownList3.Items.Add("-");
        for (int i = 0; i < 5; i++)
        {
           
            if ((yue + i) < 10)
            {
                if((yue+i)==0)
                {
                    ddlt.Items.Add(datenow + "12");
                    continue;
                }

                if ((yue + i) == -1)
                {
                    ddlt.Items.Add(datenow + "11");
                    continue;
                }

                if ((yue + i) == -2)
                {
                    ddlt.Items.Add(datenow + "10");
                    continue;
                }
                ddlt.Items.Add(datenow + "0" + (yue + i).ToString());
            }
            else
            {
                if((yue+i)>12)
                {
                    if (tempyue == 0)
                    {
                        tempyue = 1;
                        ddlt.Items.Add(datenow + "0" + tempyue.ToString());
                        continue;
                    }
                    else
                    {
                        yue = tempyue + 1;
                    }
                }

                if ((yue + i) < 10)
                {
                    ddlt.Items.Add(datenow + "0" + (yue + i).ToString());
                    continue;
                }
                ddlt.Items.Add(datenow + (yue + i).ToString());
            }
           
        }
       
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }


    /// <summary>
    /// 绑定GridView2
    /// </summary>
    /// <param name="jixiaodate"></param>
    public void GetGrid2DataBind(string jixiaodate)
    {
        DataSet ds = wt.GetDataSet("select a.userid, b.username,a.sumtotal,a.JiXiaoDate from (select  userid,JiXiaoDate,SUM(total) as sumtotal from finance.dbo.JiXiao_ContractAmount group by userid,JiXiaoDate) as a left outer join webclipping.dbo.users as b on a.userid=b.userid where a.JiXiaoDate='" + jixiaodate + "'", wt.GetConnection());
        this.GridView2.DataSource = ds.Tables[0];
        this.GridView2.DataBind();
    }

    /// <summary>
    /// 绑定GridView3
    /// </summary>
    /// <param name="jixiaodate"></param>
    public void GetGrid3DataBind()
    {
        DataSet ds = wt.GetDataSet("select a.billid,a.account_name,a.company,c.username,a.total as a_total,b.total as b_total,(a.total-b.total) as cha from finance.dbo.View_Bill as a inner join (select userid, billid,sum(total) as total from finance.dbo.JiXiao_ContractAmount group by finance.dbo.JiXiao_ContractAmount.billid,userid) as b on a.billid=b.billID and a.total<>b.total left outer join webclipping.dbo.users as c on b.userid=c.userid", wt.GetConnection());
        this.GridView3.DataSource = ds.Tables[0];
        this.GridView3.DataBind();
        MergeRows(GridView3);
    }

    /// <summary>
    /// 加载差值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnShowDiff_Click(object sender, EventArgs e)
    {
        GetGrid3DataBind();
        ReLoad();
    }

    /// <summary>
    /// 加载绩效金额
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
     protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = DropDownList5.SelectedItem.Text;
        GetGrid2DataBind(str+"-01");
        ReLoad();
        this.GridView4.DataSource = null;
        this.GridView4.DataBind();
    }
       
   

    /// <summary>
    /// 合并GridView重复值
    /// </summary>
    /// <param name="gridView"></param>
    public  void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 1; i < row.Cells.Count; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                           previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }


    /// <summary>
    /// 合并GridView重复值new
    /// </summary>
    /// <param name="gridView"></param>
    public void MergeRows_New(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count-3; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                           previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
    /// <summary>
    /// 提交差异数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTjDiff_Click(object sender, EventArgs e)
    {
        string str = DropDownList4.SelectedItem.Text.ToString();
        for (int i = 0; i < this.GridView3.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)this.GridView3.Rows[i].FindControl("CheckBox2");

            if (cb.Checked == true)
            {
                try
                {
                    Differencestreatment(this.GridView3.DataKeys[i]["billid"].ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        ReLoad();
    }


    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string userid= this.GridView2.DataKeys[this.GridView2.SelectedIndex]["userid"].ToString();
        string sql = "select a.billid,a.accountid,b.account_name,a.companyid,c.company,a.userid,d.username,a.total,a.operateDate from finance.dbo.JiXiao_ContractAmount  as a left outer join account as b on a.accountid=b.accountid left outer join Company as c on a.companyid=c.companyID left outer join users as d on a.userid=d.userid where a.JiXiaoDate='" + DropDownList5.SelectedItem.Text.Trim() +"' and userid='"+userid+"'";
        DataSet ds = wt.GetDataSet(sql, wt.GetConnection());
        this.GridView4.DataSource = ds.Tables[0];
        this.GridView4.DataBind();
        MergeRows(GridView4);
    }


    /// <summary>
    ///绑定Gridview4
    /// </summary>
    /// <param name="userid"></param>

    public void GetGrid4DataBind(string userid)
    {
        //string userid = this.GridView2.DataKeys[this.GridView2.SelectedIndex]["userid"].ToString();
        string sql = "select a.billid,a.accountid,b.account_name,a.companyid,c.company,a.userid,d.username,a.total,a.zhangdanDate,a.operateDate from finance.dbo.JiXiao_ContractAmount  as a left outer join finance.dbo.account as b on a.accountid=b.accountid left outer join finance.dbo.Company as c on a.companyid=c.companyID left outer join users as d on a.userid=d.userid where a.JiXiaoDate='" + DropDownList5.SelectedItem.Text.Trim() + "-01" + "' and a.userid='" + userid + "'";
        DataSet ds = wt.GetDataSet(sql, wt.GetConnection());
        this.GridView4.DataSource = ds.Tables[0];
        this.GridView4.DataBind();
        MergeRows_New(GridView4);
    }

    /// <summary>
    /// GridView2行命令
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "lookmx")
        {
            GridViewRow dvr = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
            string userid = this.GridView2.DataKeys[dvr.RowIndex]["userid"].ToString();
            GetGrid4DataBind(userid);
            ReLoad();
        }
    }
}

 

你可能感兴趣的:(代码)