<asp:TemplateColumn HeaderText="genre (database)"> <ItemTemplate> <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.genre") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList id=DropDownList1 runat="server" DataSource="<%# GetGenreTable() %>" DataMember="Genre" DataTextField="genre" DataValueField="genre" Width="120px"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateColumn> Preselecting an Item in the Drop-Down List You often want to set the selected item in the drop-down list to match a specific value, usually the value displayed in the cell in display mode. You can do this by setting the SelectedIndex property of the drop-down list to the index of the value to display.
The following example shows a reliable way to do this in a handler for the DataGrid item's ItemDataBound event. This is the correct event to use, because it guarantees that the drop-down list has already been populated, no matter what data source the drop-down list is using.
The trick is in knowing what value to set the drop-down list to. Typically, the value is already available to you either in the current item (being displayed) or in the DataItem property of the current item, which returns a DataRowView object containing the current record. Once you have the value, you can use the DropDownList control's FindByText or FindByValue method to locate the correct item in the list; you can then use the item's IndexOf property to return the index.
' Visual Basic Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _ Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.EditItem Then Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) Dim currentgenre As String = CType(drv("genre"), String) Dim ddl As DropDownList ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList) ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(currentgenre)) End If End Sub