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.