I am using vs2002 and am able to populate a dropdownlist dynamically in a datagrid when I click the edit button. My next issue is in Edit mode to have the value selected in the EditItemTemplate dropdownlist called ddlABCD_edit.

I have provided what I think is all the code used for this column. Anyone have a sample or see an easy fix for me?

If I have the following values:
Red
Green
Blue

and for that record in the datagrid is Green, I need Green to be selected in the dropdownlist.

VB Code:
  1. <asp:datagrid id="dgrdAccountInfo" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="850px" HorizontalAlign="Center" OnEditCommand="dgrdAccountInfo_EditCommand"
  2.                     OnCancelCommand="dgrdAccountInfo_CancelCommand"
  3.                     OnUpdateCommand="dgrdAccountInfo_UpdateCommand" OnDeleteCommand="dgrdAccountInfo_DeleteCommand"
  4.                     DataKeyField="Account_ID" OnItemCommand="doInsert">
  5.                             <AlternatingItemStyle Font-Size="X-Small" Font-Names="Arial" BackColor="#ECECEC"></AlternatingItemStyle>
  6.                             <ItemStyle Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" BackColor="White"></ItemStyle>
  7.                             <HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="#4A7184"></HeaderStyle>
  8.                             <Columns>
  9. '---------- REMOVED COLUMNS ----------------
  10. <asp:TemplateColumn HeaderText="SCAC" SortExpression="ABCD">
  11.     <HeaderStyle HorizontalAlign="Center" Width="75px"></HeaderStyle>
  12.     <ItemStyle Font-Size="X-Small" HorizontalAlign="Center"></ItemStyle>
  13.     <ItemTemplate>
  14.         <asp:Label id= "lblABCD" runat= "server" Text= '<%# Container.DataItem("ABCD") %>'>
  15.         </asp:Label>
  16.     </ItemTemplate>
  17.     <EditItemTemplate>                            
  18.         <asp: dropDownList runat="server" id="ddlABCD_edit" DataValueField="CarrierID" DataTextField="ABCD" DataSource='<%# GetABCD() %>' />
  19.     </EditItemTemplate>
  20.     <FooterTemplate>
  21.         <asp: dropDownList id="ddlABCD_add" runat="server"></asp: dropDownList>
  22.     </FooterTemplate>
  23. </asp:TemplateColumn>
  24. '---------- REMOVED COLUMNS ----------------
  25. <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Updt" HeaderText="Edit" ButtonType="PushButton">
  26.                                     <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
  27.                                 </asp:EditCommandColumn>
  28. </Columns>
  29. </asp:datagrid>

Code from the codebehind
VB Code:
  1. 'Create a connection
  2. Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("company.DB"))
  3. Dim ddlDataSet As DataSet = New DataSet()
  4.  
  5. Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
  6.         dgrdAccountInfo.ShowFooter = False
  7.         dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex
  8.         sbFillDataGrid()
  9. End Sub
  10.  
  11. Function GetABCD() As DataSet
  12.     'Populate the ddlDataSet
  13.     Const sSQL As String = "SELECT CarrierID, ABCD FROM tblTABLENAME ORDER BY ABCD"
  14.  
  15.    Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sSQL, myConnection)
  16.  
  17.    myDataAdapter.Fill(ddlDataSet, "tblTABLENAME")
  18.  
  19.    Return ddlDataSet
  20. End Function