Click here to Skip to main content
14,429,185 members
Rate this:
Please Sign up or sign in to vote.
See more:
'ddl1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

here is my code
Dim da As New SqlDataAdapter("select * from item_master", con)
                Dim drdList As DropDownList

                For Each grdRow As GridViewRow In GridView2.Rows
                    drdList = DirectCast(GridView2.Rows(grdRow.RowIndex).Cells(2).FindControl("ddl1"), DropDownList)

                    drdList.DataSource = dt
                    drdList.DataValueField = "item_id"
                    drdList.DataTextField = "units"
                    drdList.SelectedValue = GridView2.DataKeys(grdRow.RowIndex)("units").ToString()

i have written the above code in

Please help me to get out of this.
Thanks in advance
Updated 23-Mar-12 22:11pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("datasource").ConnectionString)
            Dim da1 As New SqlDataAdapter("select * from item_master", con)
            Dim dtCities As New DataTable()
            'Check if it is a DataRow.
            'Cast the row item as DatarowView
            Dim drv As DataRowView = TryCast(e.Row.DataItem, DataRowView)
            'Get the Value City for the Current Row
            Dim City As [String] = drv("units").ToString()
            'Find the DropDownList inside the Rows
            Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddl1"), DropDownList)
            'Bind the Temporory Table we have Created
            ddl.DataSource = dtCities
            'Set the Display value
            ddl.DataTextField = "units"
            'Bind the values to DropDownList
            'Find the Current City and set that as Selected
            ddl.Items.FindByText(City).Selected = True

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100