Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Quote:
'ddl1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

here is my code
VB
Dim da As New SqlDataAdapter("select * from item_master", con)
                da.Fill(dt)
                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.DataBind()
                    drdList.SelectedValue = GridView2.DataKeys(grdRow.RowIndex)("units").ToString()
                Next

i have written the above code in
GridView2_RowDataBound

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

1 solution

VB
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()
            da1.Fill(dtCities)
            '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
            ddl.DataBind()
            'Find the Current City and set that as Selected
            ddl.Items.FindByText(City).Selected = True
            '
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900