DropDownList has a SelectedValue which is invalid because
Posted: Thu Nov 09, 2006 4:12 pm
BB
Regular
Joined: Jun 23, 2004
Posts: 340
Here is the scenario, I was using a gridview control in visual studio 2005. I had an itemtemplate with a label for the value and an edititemtemplate with a dropdownlist to use to select values. Unfortunately, the actual row in the main table did not have a corresponding entry in the source table for the drop down list. So I got an error:
Quote:
System.ArgumentOutOfRangeException: DropDownList1 has a SelectedValue
which is invalid because it does not exist in the list of items.
This is quite a problem if you have not maintained data integrity, and/or if your Select statment in your dropdownlist data source does not include particular values.
My problem was that my main table had a key in the product manager field for a user in the user table who was not flagged as a product manager.
After much scratching and fumbling, I eventually decided on two possibilities.
Plan A.)
1.) In the RowEditing event, grab the current person from the list and store in a private member variable
Code:
Dim WhichRow As Integer = e.NewEditIndex
m_SelectedManager = CType(CType(grvProduct.Rows(WhichRow).Cells(4).Controls.Item(1), System.Web.UI.Control), System.Web.UI.WebControls.Label).Text()
2.) Then use that member variable in a SQL statement to look up the corresponding ID.
3.) Find the drop down list in the Gridview databinding event and insert the missing item. The rowstate for the edited row will be edit (4) or alternate(1) + edit(4) = 5
Code:
If ((e.Row.RowState = 4) Or (e.Row.RowState = 5)) Then
Dim MydrpEditMgrId As DropDownList = CType(e.Row.Cells(4).FindControl("drpEditMgrId"), DropDownList)
Dim myUserID As Integer = GetUserIDbyName(m_SelectedManager)
Dim myListItem = New ListItem(m_SelectedManager & " *", myUserID)
Try
MydrpEditMgrId.Items.Add(myListItem)
Catch
End Try
Try
MydrpEditMgrId.SelectedValue = myUserID
Catch
End Try
End If
Plan B
Posted: Thu Nov 09, 2006 4:16 pm
BB
Regular
Joined: Jun 23, 2004
Posts: 340
Plan B.)
This turned out to be the final solution, because the user was interested in having all the users available. Change the SELECT statement to be a UN-ION of both the source table and the list of all the entries in the main table that would not have appeared in the source table.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest (c) 2006 by Group29 Productions.
You can syndicate Group29 Productions news with an RSS Feeder using the file backend.php