简体   繁体   中英

Gridview Binding DropDownList from code behind

I need to bind grid view with check box column, droupdown column and text box. it is like default mode is edit. please help me to bind combo box items from code behind. Combo box values need to get from web service. all the grid data binding from code behind. user can change the data on grid.

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
        <asp:TemplateField >
                <ItemTemplate >
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked= "<%# Bind('select') %>" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Roles">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" 
                        SelectedValue='<%# Eval("Roles") %>'>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text="<%# Bind('Name') %>"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

code behind

 protected void Page_Load(object sender, EventArgs e)
{

    DataTable table = new DataTable();
    table.Columns.Add("select", typeof(bool));
    table.Columns.Add("Roles", typeof(string));
    table.Columns.Add("Name", typeof(string));
    table.Rows.Add(true, "Admin", "name1" );
    table.Rows.Add(true, "Admin", "name2");
    table.Rows.Add(true, "user", "name3");
    table.Rows.Add(false, "user", "name4");
    table.Rows.Add(false, "Admin", "name5");
    GridView1.DataSource = table;
    GridView1.DataBind();

}

You could try something like this.

<asp:DropDownList ID="DropDownList1" runat="server" 
OnDataBound="DropDownList_OnDataBound"></asp:DropDownList>

and

protected void DropDownList_OnDataBound(object sender, EventArgs e)

{
   DropDownList ddl = sender as DropDownList;
   if(ddl != null)
   {
        // call web service and 
        // populate ddl.Items
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM