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:
<asp:datagrid id="dgrdAccountInfo" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="850px" HorizontalAlign="Center" OnEditCommand="dgrdAccountInfo_EditCommand" OnCancelCommand="dgrdAccountInfo_CancelCommand" OnUpdateCommand="dgrdAccountInfo_UpdateCommand" OnDeleteCommand="dgrdAccountInfo_DeleteCommand" DataKeyField="Account_ID" OnItemCommand="doInsert"> <AlternatingItemStyle Font-Size="X-Small" Font-Names="Arial" BackColor="#ECECEC"></AlternatingItemStyle> <ItemStyle Font-Size="X-Small" Font-Names="Arial" VerticalAlign="Top" BackColor="White"></ItemStyle> <HeaderStyle Font-Size="10pt" Font-Names="Arial" Font-Bold="True" ForeColor="White" BackColor="#4A7184"></HeaderStyle> <Columns> '---------- REMOVED COLUMNS ---------------- <asp:TemplateColumn HeaderText="SCAC" SortExpression="ABCD"> <HeaderStyle HorizontalAlign="Center" Width="75px"></HeaderStyle> <ItemStyle Font-Size="X-Small" HorizontalAlign="Center"></ItemStyle> <ItemTemplate> <asp:Label id= "lblABCD" runat= "server" Text= '<%# Container.DataItem("ABCD") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp: dropDownList runat="server" id="ddlABCD_edit" DataValueField="CarrierID" DataTextField="ABCD" DataSource='<%# GetABCD() %>' /> </EditItemTemplate> <FooterTemplate> <asp: dropDownList id="ddlABCD_add" runat="server"></asp: dropDownList> </FooterTemplate> </asp:TemplateColumn> '---------- REMOVED COLUMNS ---------------- <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Updt" HeaderText="Edit" ButtonType="PushButton"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> </asp:EditCommandColumn> </Columns> </asp:datagrid>
Code from the codebehind
VB Code:
'Create a connection Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("company.DB")) Dim ddlDataSet As DataSet = New DataSet() Sub dgrdAccountInfo_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) dgrdAccountInfo.ShowFooter = False dgrdAccountInfo.EditItemIndex = e.Item.ItemIndex sbFillDataGrid() End Sub Function GetABCD() As DataSet 'Populate the ddlDataSet Const sSQL As String = "SELECT CarrierID, ABCD FROM tblTABLENAME ORDER BY ABCD" Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(sSQL, myConnection) myDataAdapter.Fill(ddlDataSet, "tblTABLENAME") Return ddlDataSet End Function




Reply With Quote