温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-09 07:18:21 来源:网络 阅读:735 作者:坏蛋栀子花 栏目:编程语言

前台:

       <div>
     标题控件:<br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCreated="GridView1_RowCreated">
            <Columns>
                <asp:BoundField DataField="M_Name" HeaderText="名称" />
                <asp:BoundField DataField="M_ID" HeaderText="字段ID" />
            </Columns>
        </asp:GridView>
       <br /> 多标题:<br />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowCreated="GridView2_RowCreated">
            <Columns>
                <asp:BoundField DataField="M_ID" HeaderText="字段ID1" />
                <asp:BoundField DataField="M_ID" HeaderText="字段ID" />
                <asp:BoundField DataField="M_Name" HeaderText="名称" />
                <asp:BoundField DataField="M_Name" HeaderText="名称1" />
            </Columns>
        </asp:GridView>
    </div>

后台绑定:

    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 = "</br>" + 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 += "<strong>" + 1111 + "</strong>";
                tcHeader[0].Text += "天</th></tr><tr>";

                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 = "密码</th></tr><tr>";
            }
        }

    

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI