溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

gridview多標(biāo)題和后臺生成標(biāo)題控件

發(fā)布時間:2020-07-09 07:18:21 來源:網(wǎng)絡(luò) 閱讀:724 作者:壞蛋梔子花 欄目:編程語言

前臺:

       <div>
     標(biāo)題控件:<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 /> 多標(biāo)題:<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的表頭的自內(nèi)容
                    DropDownList dro = new DropDownList();//創(chuàng)建表頭下拉框
                    dro.ID = "dro" + i.ToString();//表頭ID
                    dro.Items.Insert(0, new ListItem("--請選擇--", "-1"));
                    dro.Items.Add(new ListItem("數(shù)學(xué)","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++;
                }
            }

        }

 

多標(biāo)題:

 

    

    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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI