gridview多标题和后台生成标题控件

前台:

创新互联专注于河曲企业网站建设,响应式网站,商城网站建设。河曲网站建设公司,为河曲等地区提供建站服务。全流程定制设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

       


     标题控件:
        
            
                
                
            

        
        多标题:
        
            
                
                
                
                
            

        
    

后台绑定:

    private void bing()
        {
            string sql = "select M_ID,M_Name from Module";
            DataTable dt = new DataTable();
            dt = DBArticle.RunSqlDt(sql);
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
            GridView2.DataSource = dt.DefaultView;
            GridView2.DataBind();
        }

 

在表头添加下拉框 :

    

      protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)//是否是表头行
            {
                TableCellCollection tcl = e.Row.Cells;//获得表头元素的实例
                int i = 1;
                foreach (TableCell item in tcl)
                {
                    string HeadText = item.Text;//本来datatable的表头的自内容
                    DropDownList dro = new DropDownList();//创建表头下拉框
                    dro.ID = "dro" + i.ToString();//表头ID
                    dro.Items.Insert(0, new ListItem("--请选择--", "-1"));
                    dro.Items.Add(new ListItem("数学","0"));
                    dro.Items.Add(new ListItem("语文", "1"));
                    dro.Items.Add(new ListItem("英语", "2"));

                    item.Controls.Add(dro);//添加下拉框控件

                    Label lab = new Label();
                    lab.ID = "lab" + i.ToString();
                    lab.Text = "
" + HeadText;
                    item.Controls.AddAt(1, lab);//此刻表头单元格已有下拉框的label控件
                    i++;
                }
            }

        }

 

多标题:

 

    

    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();

                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].ColumnSpan = 4;//合并第一行的4列
                tcHeader[0].Text = "截止到今日还有";
                tcHeader[0].Text += "" + 1111 + "";
                tcHeader[0].Text += "天";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Style.Add("color", "#004274");
                tcHeader[1].Style.Add("height", "30px");
                tcHeader[1].ColumnSpan = 2;
                tcHeader[1].Text = "姓名";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Style.Add("color", "#004274");
                tcHeader[2].Style.Add("height", "30px");
                tcHeader[2].ColumnSpan = 2;
                tcHeader[2].Text = "密码";
            }
        }

    

 


文章标题:gridview多标题和后台生成标题控件
文章分享:http://ybzwz.com/article/gjpspj.html