简体   繁体   中英

Drop Down List In A Gridview

I have a GridView , inside the GridView I have a template field and inside that, a drop down list.

<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="Hello" runat="server">
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

I want to databind the GridView but how do I make the drop down list change its value to according to the information I gave it while databinding?

Im used to using DataField in bound fields

<asp:BoundField HeaderText="Hello" DataField="HelloDB" />

All you have to do is tap into the OnRowDataBind event of the GridView . Within that, you can use FindControl() to get the drop down, cast it as a DropDown , then set the value.

This event is called when each row is databound, so each dropdown would be updated.

Microsoft provides a walk-through on this.

and quick Bing search comes up with many other articles and how-to's.

Example:

protected void MethodName(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
    {
     DropDownList Hello = e.Row.FindControl("Hello") as DropDownList;
     //here you can bind the dropdown list.

    }
}

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