动态添加gridView 并绑定数据

来源:互联网 发布:sql developer登录 编辑:程序博客网 时间:2024/05/29 03:21

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.Collections.Generic;
using DisDateDAL;
using System.Data.SqlClient;

public partial class zldxmx : System.Web.UI.Page
{
    private static string symbol = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string time1 = Request.QueryString["tdate"];
            symbol = Request.QueryString["symbol"];
            string time = this.lblCurrentTime.Text;
            //加载页面显示数据到Gridview 中
            string sqlddldowmlist = "select distinct SNAME,Convert(varchar(10),TDATE,120) as TDATE,SYMBOL from tb_7035 where TDATE='" + time1 + "'  ";
            bind(time1, symbol);
        }

    }

    /// <summary>
    /// 动态添加GridView
    /// </summary>
    /// <param name="list7036"></param>
    /// <param name="count"></param>
    public void addGridView(IList<tb_7036> list7036, int count, string day,IList<string> listtype)
    {
        GridView GV = new GridView();
        GV.ID = "gridview" + count;//设置GridView的Id
        GV.Attributes.Add("runat", "server");
        GV.AutoGenerateColumns = false;//不自动生成列
        GV.Attributes.Add("bgcolor", "#FAF4EB");//背景颜色
        GV.Width = 903;//宽度
        GV.Attributes.Add("Font-Size", "12px");//字体大小
        GV.BorderWidth = 1;//边框宽度
        GV.HeaderStyle.Font.Size = 10;//头部字体大小
        GV.FooterStyle.Height = 20;//脚部高度


        GV.HeaderStyle.BackColor = System.Drawing.Color.FromName("#FCF6D1");//头部背景颜色
        GV.BorderColor = System.Drawing.Color.FromName("#B5AF94");//gridview边框颜色

        TemplateField template6 = new TemplateField();
        template6.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列6");
        template6.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        template6.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template6.ItemStyle.Height = 20;
        template6.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template6.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template6.HeaderText = "序号";
        GV.Columns.Add(template6);


        BoundField boundField2 = new BoundField();//第二列
        GV.Columns.Add(boundField2);
        boundField2.HeaderText = "买卖方向";
        boundField2.DataField = "F0040";
        boundField2.ItemStyle.Width = 80;
        boundField2.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        boundField2.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        boundField2.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");

        //动态添加模板列
        TemplateField template1 = new TemplateField();//第三列为模板列
        template1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列1");
        template1.HeaderText = "营业部名称";
        template1.FooterStyle.HorizontalAlign = HorizontalAlign.Center;
        template1.ItemStyle.Width = 300;
        template1.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template1.FooterText = "合计*";//脚部的文本显示
        template1.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template1.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        GV.Columns.Add(template1);

        TemplateField template2 = new TemplateField();
        template2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列2");
        template2.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        template2.ItemStyle.Width = 100;
        template2.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template2.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template2.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template2.HeaderText = "营业部性质";
        GV.Columns.Add(template2);

        TemplateField template3 = new TemplateField();
        template3.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列3");
        template3.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        template3.ItemStyle.Width = 115;
        template3.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template3.HeaderText = "买入金额(万元)";
        template3.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template3.FooterText = list7036[0].F0110.ToString();
        template3.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
        template3.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        GV.Columns.Add(template3);

        TemplateField template4 = new TemplateField();
        template4.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列4");
        template4.HeaderText = "卖出金额(万元)";
        template4.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        template4.ItemStyle.Width = 115;
        template4.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
        template4.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template4.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template4.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template4.FooterText = list7036[0].F0120.ToString();
        GV.Columns.Add(template4);

        TemplateField template5 = new TemplateField();
        template5.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "列5");
        template5.HeaderText = "买卖比*";
        template5.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        template5.FooterText = list7036[0].F0130.ToString();
        template5.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
        template5.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template5.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        template5.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        GV.Columns.Add(template5);

        BoundField boundField8 = new BoundField();
        GV.Columns.Add(boundField8);
        boundField8.HeaderText = "占比(%)*";
        boundField8.ItemStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        boundField8.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        boundField8.HeaderStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        boundField8.FooterStyle.BorderColor = System.Drawing.Color.FromName("#B5AF94");
        boundField8.DataField = "F0100";

        GV.ShowFooter = true;
     
        List<tb_7036> list=new List<tb_7036>() ;
        for (int i = 0; i < list7036.Count; i++)
        {
            int flat = 1;
            if (i == 0)
            {
                list.Add(list7036[0]);
                flat = 0;
            }           
            for (int j=0;j<list.Count;j++)
            {
                if (list7036[i].Smybol.Equals(list[j].Smybol) && list7036[i].F0040.Equals(list[j].F0040) && list7036[i].F0050.Equals(list[j].F0050) && list7036[i].F0070.Equals(list[j].F0070) && list7036[i].F0080.Equals(list[j].F0080)&&list7036[i].F0090.Equals(list[j].F0090)&& list7036[i].F0110.Equals(list[j].F0110)&&!list7036[i].Type.Equals(list[j].Type))
                {                  
                    flat = 0;                
                }
              
            }
            if (flat == 1)
                list.Add(list7036[i]);             
        }
      GV.DataSource = list;

 

            GV.RowDataBound += new GridViewRowEventHandler(GV_RowDataBound);
        GV.DataBind();

        //股票名称及代码
        Label lblSName = new Label();
        lblSName.Font.Size = 10;
        lblSName.Height = 20;
        lblSName.Attributes.Add("runat", "server");
        lblSName.Text = "<strong style='size:10px'>股票名称(代码):<font style='color:#FF0000'>" + list7036[0].Sname.ToString() + "(" + list7036[0].Smybol.ToString() + ")</font></strong>";
        this.placeHolder.Controls.Add(lblSName);

        Label lblf0010 = new Label();
        lblf0010.Font.Size = 10;
        lblf0010.Height = 20;
        if (list7036[0].F0010 > 0)
        {
            lblf0010.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>" + day + "日涨跌幅(%):<font color=#FF0000>" + list7036[0].F0010.ToString() + "</font></strong>"; //涨幅度
        }
        else
        {
            lblf0010.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>" + day + "日涨跌幅(%):<font color=#006633>" + list7036[0].F0010.ToString() + "</font></strong>"; //涨幅度
        }
        this.placeHolder.Controls.Add(lblf0010);

        Label lblf0110 = new Label();
        lblf0110.Font.Size = 10;
        lblf0110.Height = 20;
        lblf0110.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>" + day + "日成交金额(万元)*:" + list7036[0].F0020.ToString() + "</strong><br>";
        this.placeHolder.Controls.Add(lblf0110);

        //给动态添加的GridView 前添加一个Lable用于显示异动说明

        IList<string> listyidong = new List<string>();


        Label lblshuoming = new Label();
        lblshuoming.Width = 903;
        lblshuoming.Height = 15;
        lblshuoming.Font.Size = 10;
        lblshuoming.BackColor = System.Drawing.Color.FromName("#B5AF94");

        lblshuoming.Text = "<strong>异动说明: ";

        if (listtype.Count > 1)
        {
            for (int j = 0; j < listtype.Count; j++)
            {
                if (j == listtype.Count - 1)
                {
                    lblshuoming.Text += listtype[j].ToString() + "</strong>";
                }
                else
                    lblshuoming.Text += listtype[j].ToString() + "/";
            }
        }
        else
            lblshuoming.Text += listtype[0].ToString() + "</strong>";

        this.placeHolder.Controls.Add(lblshuoming);
        this.placeHolder.Controls.Add(GV);
        Label a = new Label();
        a.Height = 12;
        a.Width = 903;

        this.placeHolder.Controls.Add(a);

        this.lblCurrentTime.Text = simpledateformate(list7036[0].TDate.ToString());//当前时间


    }
    public string simpledateformate(string date)
    {
        string[] dates = date.Split('-');
        return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
    }
    void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        int i = e.Row.RowIndex + 1;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //将序号自动增长
            e.Row.Cells[0].Text = i.ToString();

        }

    }
    /// <summary>
    /// 样板类产生器,以创建样板字段
    /// </summary>

    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, string colname)
        {
            templateType = type;
            columnName = colname;

        }
        //实现了ITemplate接口的方法
        public void InstantiateIn(System.Web.UI.Control container)
        {
            if (templateType == DataControlRowType.DataRow)
            {
                //指定样板字段的数据绑定事件
                switch (columnName)
                {
                    case "列1":
                        Label lblSName = new Label();
                        lblSName.DataBinding += new EventHandler(lblSName_DataBinding);
                        container.Controls.Add(lblSName);
                        break;
                    case "列2":
                        Label lblxingzhi = new Label();
                        lblxingzhi.DataBinding += new EventHandler(lblxingzhi_DataBinding);
                        container.Controls.Add(lblxingzhi);
                        break;
                    case "列3":
                        Label lblinM = new Label();
                        lblinM.DataBinding += new EventHandler(lblinM_DataBinding);
                        container.Controls.Add(lblinM);
                        break;
                    case "列4":
                        Label lbloutM = new Label();
                        lbloutM.DataBinding += new EventHandler(lbloutM_DataBinding);
                        container.Controls.Add(lbloutM);
                        break;
                    case "列5":
                        Label lblMM = new Label();
                        lblMM.DataBinding += new EventHandler(lblMM_DataBinding);
                        container.Controls.Add(lblMM);
                        break;
                    case "列6":
                        Label lblxu = new Label();
                        lblxu.DataBinding += new EventHandler(lblxu_DataBinding);
                        container.Controls.Add(lblxu);
                        break;
                }

            }

        }

        void lblxu_DataBinding(object sender, EventArgs e)
        {
            Label lblxu = (Label)sender;
            lblxu.Text = "";

        }


        void lblMM_DataBinding(object sender, EventArgs e)
        {
            Label lblMM = (Label)sender;
            GridViewRow row = (GridViewRow)lblMM.NamingContainer;
            if (DataBinder.Eval(row.DataItem, "F0090").ToString() == "0")
                lblMM.Text = "";
            else
                lblMM.Text = DataBinder.Eval(row.DataItem, "F0090").ToString();

        }


        void lbloutM_DataBinding(object sender, EventArgs e)
        {
            Label lbloutM = (Label)sender;
            GridViewRow row = (GridViewRow)lbloutM.NamingContainer;
            if (DataBinder.Eval(row.DataItem, "F0080").ToString() == "0")
                lbloutM.Text = "";
            else
                lbloutM.Text = DataBinder.Eval(row.DataItem, "F0080").ToString();
            string sub = symbol.Substring(0, 1);
            if (sub.Equals("0"))
            {
                if (lbloutM.Text == "")
                    lbloutM.Text = "0";
            }
        }

        void lblinM_DataBinding(object sender, EventArgs e)
        {
            Label lblintM = (Label)sender;
            GridViewRow row = (GridViewRow)lblintM.NamingContainer;
            if (DataBinder.Eval(row.DataItem, "F0070").ToString() == "0")
                lblintM.Text = "";
            else
                lblintM.Text = DataBinder.Eval(row.DataItem, "F0070").ToString();

            //设置对于深圳的股票买入和卖出的如果数据为0的则显示为0,对于上海的股票如果买入和卖出的股票为0的则显示空

            string sub = symbol.Substring(0, 1);
            if (sub.Equals("0"))
            {
                if (lblintM.Text == "")
                    lblintM.Text = "0";
            }
        }

        void lblxingzhi_DataBinding(object sender, EventArgs e)
        {
            Label lblxzh = (Label)sender;
            GridViewRow row = (GridViewRow)lblxzh.NamingContainer;
            if (DataBinder.Eval(row.DataItem, "F0060").ToString() == "")
                lblxzh.Text = "--";
            else
                lblxzh.Text = DataBinder.Eval(row.DataItem, "F0060").ToString();

        }
        //样板的DataBind  
        private void lblSName_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            CheckBox c = new CheckBox();
            GridViewRow row = (GridViewRow)lbl.NamingContainer;
            lbl.Text = DataBinder.Eval(row.DataItem, "F0050").ToString();
        }
    }

    /// <summary>
    ///  获得7035 表中所有的数据
    /// </summary>
    public void bind(string time, string symbol)
    {
        //给GridView  赋值
        string sql = "select distinct f0050  from tb_7036 where symbol='" + symbol + "' and tdate='" + time + "'";
        IList<int> listDayCount = new List<int>();//用于存放天数的集合

        DataTable dtNum = DBHelper.GetDataSet(sql);
        if (dtNum.Rows.Count != 0)
        {
            int count = 0;
            foreach (DataRow dr in dtNum.Rows)
            {
                count++;
                string sql1 = "select distinct f0030 from tb_7035 where type in (select Type from tb_7036 where  symbol='" + symbol + "' and tdate='" + time + "' and f0050='" + dr[0].ToString() + "') and  symbol='" + symbol + "' and tdate='" + time + "'";
                List<string> listtype = new List<string>();
                DataTable dt =  DBHelper.GetDataSet(sql1);
                foreach (DataRow drtype in dt.Rows)
                {
                    listtype.Add(drtype[0].ToString ());
                }
            

                string sql2 = "SELECT TOP 100* FROM (select TOP 1000 Convert(varchar(10),TDATE,120) as TDATE,SYMBOL,SNAME,TYPE,round(F0010,2) as F0010,F0020,F0030,F0040,F0050,F0060,round(F0070/10000,2) as F0070,round(F0080/10000,2) as F0080,F0090,round(F0100,2) as F0100,round(F0110/10000,2) as F0110,round(F0120/10000,2) as F0120,round(F0130,2) as F0130,STATUS,ID from tb_7035 A where symbol='" + symbol + "' and tdate='" + time + "' and  type in (select top 1 TYPE from tb_7036 where symbol='" + symbol + "' and tdate='" + time + "' and  f0050='" + dr[0].ToString() + "') and f0040='买入'order by F0040 asc,F0070 desc) AA union all SELECT TOP 100* FROM (select TOP 1000 Convert(varchar(10),TDATE,120) as TDATE,SYMBOL,SNAME,TYPE,round(F0010,2) as F0010,F0020,F0030,F0040,F0050,F0060,round(F0070/10000,2) as F0070,round(F0080/10000,2) as F0080,F0090,round(F0100,2) as F0100,round(F0110/10000,2) as F0110,round(F0120/10000,2) as F0120,round(F0130,2) as F0130,STATUS,ID from tb_7035 A where symbol='" + symbol + "' and tdate='" + time + "' and  type in (select top 1 TYPE from tb_7036 where symbol='" + symbol + "' and tdate='" + time + "' and  f0050='" + dr[0].ToString() + "') and f0040='卖出'order by F0040 asc,F0080 desc) AA";
            

                addGridView(get7036data(sql2), count, dr[0].ToString(), listtype);

            }
        }
    }
    /// <summary>
    /// 根据SQL 语句返回一个集合
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>

    public IList<tb_7036> get7036data(string sql)
    {
        IList<tb_7036> list7036 = null;
        DataTable dt = DBHelper.GetDataSet(sql);
        foreach (DataRow dr in dt.Rows)
        {
            tb_7036 tb7036 = new tb_7036();
            tb7036.TDate = dr["TDATE"].ToString();
            tb7036.Smybol = dr["SYMBOL"].ToString();
            tb7036.Sname = dr["SName"].ToString();
            if (dr["F0010"].ToString() != "")
                tb7036.F0010 = float.Parse(dr["F0010"].ToString());
            if (dr["F0020"].ToString() != "")
                tb7036.F0020 = float.Parse(dr["F0020"].ToString());
            tb7036.F0030 = dr["F0030"].ToString();
            tb7036.F0040 = dr["F0040"].ToString();
            tb7036.F0050 = dr["F0050"].ToString();
            tb7036.F0060 = dr["F0060"].ToString();
            if (dr["F0070"].ToString() != "")
                tb7036.F0070 = float.Parse(dr["F0070"].ToString());
            if (dr["F0080"].ToString() != "")
                tb7036.F0080 = float.Parse(dr["F0080"].ToString());
            if (dr["F0090"].ToString() != "")
                tb7036.F0090 = float.Parse(dr["F0090"].ToString());
            if (dr["F0100"].ToString() != "")
                tb7036.F0100 = float.Parse(dr["F0100"].ToString());
            if (dr["F0110"].ToString() != "")
                tb7036.F0110 = float.Parse(dr["F0110"].ToString());
            if (dr["F0120"].ToString() != "")
                tb7036.F0120 = float.Parse(dr["F0120"].ToString());
            if (dr["F0130"].ToString() != "")
                tb7036.F0130 = float.Parse(dr["F0130"].ToString());
            tb7036.Type = dr["TYPE"].ToString();
            if (list7036 == null)
            {
                list7036 = new List<tb_7036>();
            }
            list7036.Add(tb7036);
        }
        return list7036;
    }

}