Or Betzalel Or Betzalel - 14 days ago 6
ASP.NET (C#) Question

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" />

Answer

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.

Comments