View Single Post
  #1 (permalink)  
Old June 24th, 2006, 09:36 AM
Aaron Edwards Aaron Edwards is offline
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default HELP!!! (DropdownList in GridView question)

I can't seem to get my DropDownList that's in a Gridview to hold onto it's selection (check it out at: http://www.scgis/org/ConferenceStore.aspx).

The GridView is based on a dataset that is created on the fly on the page's Load event.

The dropdown list is inside an Item template, and is declared and bound as follows:

                                    <asp:TemplateField HeaderText="Size" >

                                        <ItemTemplate>
                                          <asp:DropDownList ID="ddlSize" DataTextField="Size" DataValueField="Size" runat="server" DataSource='<%# PopulateControls(Eval("ProductName"))%>' >

                                          </asp:DropDownList>
                                        </ItemTemplate>

                                    </asp:TemplateField>

The PopulateControls method looks like this:

    Public Function PopulateControls(ByVal ProductName As String) As DataSet
        Dim cnxn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        cnxn.ConnectionString = System.Configuration.ConfigurationManager.AppSetti ngs("ConnectionString")
        cnxn.Open()
        cmd.Connection = cnxn
        cmd.CommandText = "SELECT Size FROM tbl_SCGISMerchandise WHERE ProductName='" & ProductName & "'"
        da.SelectCommand = cmd

        da.Fill(ds, "Sizes")

        If ds.Tables(0).Rows(0)("Size").GetType Is System.Type.GetType("System.DBNull") Then
            Dim dr As DataRow = ds.Tables(0).NewRow
            dr("Size") = "N/A"
            ds.Tables(0).Rows.Add(dr)
        End If

        cmd.Dispose()
        da.Dispose()
        cnxn.Dispose()

        Return ds
    End Function

Since the dropdown is active for all items in the grid, and not just when in edit mode, I attempt to save the contents of the dropdown as follows:

            'go through gridview and save all sizes to dataset so we don't lose them
            Dim i As Integer = 0
            For Each gvr As GridViewRow In Me.gvCart.Rows
                Dim DDL As DropDownList = CType(gvr.FindControl("ddlSize"), DropDownList)
                Dim selectedItem As ListItem = DDL.SelectedItem
                Dim selectedValue As String = selectedItem.Value
                dsItemsOrdered.Tables(0).Rows(i)("Size") = selectedValue
                i += 1
            Next
            dsItemsOrdered.AcceptChanges()

(This is inside the RowCommand event of the other data grid on that page).

As you can see by checking it out live, what's happenning is that it's not returning the selectedItem from the dropdown list, but rather the first item in the list.

Please help ASAP. This thing is due in a few hours.

Thanks!

Aaron


Reply With Quote