Adjust other answers on this question, the ItemDataBound event should not be used to bind management data, do it at the management level.
In the drop-down list, specify the data binding event:
<asp:DropDownList runat="server" ID="ddlYourDDL" OnDataBinding="ddlYourDDL_DataBinding">
Then fire the OnDataBinding event:
protected void ddlYourDDL_DataBinding(object sender, System.EventArgs e) { DropDownList ddl = (DropDownList)(sender); for (int i = 1; i < 5; i++) { ddl.Items.Add(new ListItem(i.ToString(), i.ToString())); }
You should try and bind everything at the management level, and not look for things and have your own grid in order to know what it contains. Each control can take care of itself;) Thus, it is much easier to add and remove controls in your template and isolate these changes.
source share