Gridview and dropdownlist:the missing value

Do you know?

..has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Happens when you try to use a dropdownlist inside a edititemtemplate which is databound AND have not the value of the tablefield in the itemlist. Sounds complicated but is a real live scenario. Somebody changes the data to a value which you have not in your list, BANG! No way to catch the exception.

So i use now a unbound Dropdown with Listitems. Saving is done in RowUpdating with only one line of code

e.NewValues("land") = CType(GridView1.Rows(e.RowIndex).FindControl("lstLand"), DropDownList).SelectedValue

The real problems start in setting the value to display. Event is Rowcreated. First, i want to figure out what status the row have. Rowstate must be edit. The real amazing thing is, that the status list is handeld in a binary way and status values are added. So if you select and edit a row you get as result 2+4 = 6. My first approach was to compare with Edit mode which is 4. So fails. Old VB OR do the job.

If e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Selected) Then

Next point is to set  the value of drop down. You see code is marked as commeted, cause it doesnt work. If i have a value from table which is not in the itemlist, the code passes this line and fails later somewhere in gridview. Try catch is not able to get it. 

' CType(e.Row.FindControl("lstLand"), DropDownList).SelectedValue = DataBinder.Eval(e.Row.DataItem, "land").ToString()

Debuging shows that the values of drop down list and data are diffrent after passing this line of code.

Now i do a real ugly, but working, trick. First search for the data value in itemlist of dropdownlist. If found, do the selectedvalue.

If Not IsNothing(CType(e.Row.FindControl("lstLand"), DropDownList).Items.FindByValue(DataBinder.Eval(e.Row.DataItem, "land").ToString())) Then

CType(e.Row.FindControl("lstLand"), DropDownList).SelectedValue = DataBinder.Eval(e.Row.DataItem, "land").ToString()

End If

Hope this helps and if you have better solutions... make a comment.

 

Published Tuesday, October 11, 2005 10:48 PM by preishuber

Comments

# re: Gridview and dropdownlist:the missing value

Wednesday, October 12, 2005 3:42 AM by Teemu
I might also try somthing like this

***
'First way, just bit rewritten the original
Dim lstLand As DropDownList=CType(e.Row.FindControl("lstLand"), DropDownList)
Dim strValue As String = DataBinder.Eval(e.Row.DataItem, "land").ToString()

If Not IsNothing(lstLand.Items.FindByValue(strValue)) Then
lstLand.SelectedValue = strValue
End If

'Second way
Dim lstLand As DropDownList=CType(e.Row.FindControl("lstLand"), DropDownList)
Dim strValue As String = DataBinder.Eval(e.Row.DataItem, "land").ToString()

lstLand.SelectedIndex = lstland.Items.Indexof(lstLand.Items.FindByValue(strValue))
***
Maybe getting the value with use of DataBinder might also be subject to change, but anyways it works.

I didn't compile or test it, so watch for typos.

# re: Gridview and dropdownlist:the missing value

Monday, June 12, 2006 8:19 PM by DAvid
The 2nd method worked fine for me. Thanks for the ip, really helpfull! Although I don't understand why microsoft left this bug loose... it's not that tricky...

If even microsoft can't get it right, I'll go regain my happiness by playing with Ruby on Rails

# re: Gridview and dropdownlist:the missing value

Saturday, January 27, 2007 2:22 AM by BDC604

I completely agree with the June 12 post by David

Its not that we can't find work arounds for these type of problems, its the fact that we have to literally count the years go by before these issues are addressed. Why can't Microsoft fix these problems at the source?

# re: Gridview and dropdownlist:the missing value

Friday, February 23, 2007 3:37 PM by RJ

what causes this error when the items do exist.

I have been trying to fix the error but the data is corect and the items exist in the list and data -it looks like it thinks it is blank

# re: Gridview and dropdownlist:the missing value

Friday, March 02, 2007 11:54 AM by jackinthegreen

I had the same apparent problem, RJ, where even if the value was there I'd get the missiing value error. THen I found this guys article where he besically scrpas the binding, finds the databound item in the code behind on dropdown databound and does the value selection there. Worked great for me:

http://www.webswapp.com/CodeSamples/viewsource.aspx?file=~/codesamples/aspnet20/dependentlists/default.aspx

# dropdowwnlist

Friday, April 27, 2007 5:20 AM by aman

how to compare two dropdowwnlist values with each other

# re: Gridview and dropdownlist:the missing value

Monday, February 11, 2008 4:01 AM by Mahesh

This solution doesn't work for me. when i click edit it doesnot check

If e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Selected) Then

End IF

And Error is.............

'ddlCustomerCountry' has a SelectedValue which is invalid because it does not exist in the list of items.

Parameter name: value

# re: Gridview and dropdownlist:the missing value

Wednesday, March 19, 2008 7:38 AM by Ahmed Mokhtar

I have some Question to you "How to get the selected text of dropdownlist exist in gridview"

# re: Gridview and dropdownlist:the missing value

Saturday, April 12, 2008 5:05 AM by san

how to compare two dropdowwnlist values with each other

# re: Gridview and dropdownlist:the missing value

Tuesday, April 22, 2008 9:41 AM by JohnD

A little help more, for a novice.

ON the 'setting of the ListBox's selected-item', which EVENT is this code placed into?

I have tried gvw_DataBinding, gvw_DataBinding events and I get 'syntax-error' on 'e.RowIndex' is undefined property.

# re: Gridview and dropdownlist:the missing value

Friday, July 11, 2008 7:30 PM by Kurt Schroeder

Was having a bad day and your artical made it better i forgot about rowstate!!!!!! and couldn't figure out an error

Thanks!!!

# DropDownLists and the Selected Value Not In List problem

Monday, September 01, 2008 9:21 PM by DropDownLists and the Selected Value Not In List problem

Pingback from  DropDownLists and the Selected Value Not In List problem

# re: Gridview and dropdownlist:the missing value

Friday, November 07, 2008 9:05 AM by VinFris

to explain fields in my code, "driver" is the field that I am binding to in the database, "glTrucks" is my gridview, and "ddlTruckDrivers1" is my template dropdownlist that I am trying to compensate for values that have been deleted from the database.

Here's what I did to solve this:  

1. Deleted the "selected value="<%# bind("driver") %>" from the

dropdownlist. (i did not have this code in my above posting)

2. Placed "Imports System.Data" at the top of my code page to

accomodate for the "DataRowView"

3. Entered the following code in my dropdownlist_databound.  I got

this code from teh following website:

www.webswapp.com/.../default.aspx

(he has codebehind in C# or VB)

Protected Sub ddlTruckDrivers1_DataBound(ByVal sender As Object,

ByVal e As System.EventArgs)

       Dim ddl As DropDownList = DirectCast(sender, DropDownList)

       '===  add an empty item on top of the list

       Dim gvRow As GridViewRow = DirectCast(ddl.NamingContainer,

GridViewRow)

'check to see if the item exists in the gridview, if it doesn't then

it will default to the first item in the dropdownlist

       If Not gvRow.DataItem Is Nothing Then

           Dim strDriver As String = DirectCast(gvRow.DataItem,

DataRowView)("driver").ToString

           'be careful of the possibility that the value saved on

the  database does not exist

           'in the valid selections that are displayed on the list

           ddl.ClearSelection()

           Dim li As ListItem = ddl.Items.FindByValue(strDriver)

           If Not li Is Nothing Then li.Selected = True

       End If

   End Sub

4. This small piece of data to set the value of the update parameter:

   Protected Sub glTrucks_RowUpdating(ByVal sender As Object, ByVal e

As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles

glTrucks.RowUpdating

       e.NewValues("driver") =

CType(glTrucks.Rows(e.RowIndex).FindControl("ddlTruckDrivers1"),

DropDownList).SelectedValue

   End Sub

# re: Gridview and dropdownlist:the missing value

Friday, November 07, 2008 11:54 AM by Stacie

Thanks so much for this code.  I just have one question. If my dropdown list's datatextfield is different than my datavalue field what would I need to change in the code?  I keep getting an 'Object reference not set to an instance of an object.' error and I think its because of the above.  My datavalue field is "OthHoursCode" and my datatextfield is "Desc".

Thanks.

# re: Gridview and dropdownlist:the missing value

Tuesday, June 09, 2009 1:03 PM by Ian McBeth

As I'm fairly new to asp.net with more strength with T-SQL, I've solved this in SQL.

For the select statement which populates the drop down, I've created a custom SQL statment in the SQLDataSource which first counts the number of rows matching the search criteria.  If 0, a NULL based record is returned, otherwise the normal results set is provided.  i.e.:

IF (SELECT COUNT(*) FROM admin_attribute_option WHERE AttributeCode = @vAttrCode) = 0

BEGIN

SELECT

NULL AS OptionCode,

'SELECT VALUE' AS OptionDesc

END

ELSE

BEGIN

SELECT

OptionCode,

OptionDesc

FROM

admin_attribute_option

WHERE

AttributeCode = @vAttrCode

END

When it comes to your update / insert statements, remember to test for the NULL value coming back otherwise your page will error out.  e.g.

IF LEN(RTRIM(@OptionCode)) > 0

BEGIN

DELETE FROM

product_attributes

WHERE

ProdCode = @ProdCode

AND

AttributeCode = @AttributeCode

INSERT INTO

product_attributes

(

ProdCode, AttributeCode, OptionCode, LoginID

)

SELECT

@ProdCode,

@AttributeCode,

@OptionCode,

'TEST'

END

Leave a Comment

(required) 
(required) 
(optional)
(required)