Drop Down List In A Gridview
I have a GridView
, inside the GridView
I have a temp开发者_C百科late 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.
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.
}
}
Microsoft provides a walk-through on this.
and quick Bing search comes up with many other articles and how-to's.
精彩评论