ASP.NET中GridView动态添加下拉框

ASP.NET中GridView动态添加下拉框


//设计页
        <table border="1" cellpadding="0" cellspacing="0">
            <tr>
                <td style="font-weight: bold">GridView控件中动态添加下拉框列</td>
            </tr>
            <tr>
                <td>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Size="10.5pt" ForeColor="#333333">
    <Columns>
      <asp:BoundField HeaderText="标题" DataField="text"/>
    </Columns>
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#E3EAEB" />
        <EditRowStyle BackColor="#7C6F57" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
</asp:GridView>
                </td>
            </tr>
            <tr>
                <td style="font-size: 9pt; height: 26px;">
                    &nbsp;请选择要添加的下拉框列数:<asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                        <asp:ListItem>8</asp:ListItem>
                        <asp:ListItem>9</asp:ListItem>
                        <asp:ListItem>10</asp:ListItem>
                    </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加模板列" Font-Size="10pt" /></td>
            </tr>
        </table>

//后台代码

    System.Collections.ICollection CreateDataSource()
    {
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new DataColumn("id", typeof(Int32)));
        dt.Columns.Add(new DataColumn("text", typeof(string)));
        for (int i = 0; i < 6; i++)
        {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "列表" + i.ToString();
            dt.Rows.Add(dr);
        }
        DataView dv = new DataView(dt);
        return dv;
    }
    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType dcrType;
        private string columnName;
        public GridViewTemplate(DataControlRowType type, string colname)
        {
            dcrType = type;
            columnName = colname;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (dcrType)
            {
                case DataControlRowType.Header:
                    Literal literal = new Literal();
                    literal.Text = columnName;
                    container.Controls.Add(literal);
                    break;
                case DataControlRowType.DataRow:
                    DropDownList ddl = new DropDownList();
                    ddl.ID = "dropdownlist";
                    ddl.AppendDataBoundItems = true;
                    ddl.Items.Add(new ListItem("-----请选择------", ""));
                    ddl.Items.Add(new ListItem("AA", "a"));
                    ddl.Items.Add(new ListItem("BB", "b"));
                    ddl.Items.Add(new ListItem("CC", "c"));
                    container.Controls.Add(ddl);
                    break;
                default:
                    break;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int index = 1; index < GridView1.Columns.Count; index++)
        {
            GridView1.Columns[index].Visible = false;
        }
        for (int i = 0; i < Convert.ToInt32(DropDownList1.SelectedValue); i++)
        {
            TemplateField tf = new TemplateField();
            tf.ShowHeader = true;
            tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
            tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
            GridView1.Columns.Add(tf);
            GridView1.DataSource = CreateDataSource();
            GridView1.DataBind();
        }
    }

以上本人有试过,可以成功,但刷新一次就没了!根据实际情况应用 吧!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值