View Single Post
  #12 (permalink)  
Old December 1st, 2005, 06:28 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Right, that explains a lot.

null is not a valid value in the DropDownList so you can't use that as a value for the SelectedValue property.

You should implement some code for the DataBound event of the DetailsView control. Inside that event handler, get a reference to the View's DataItem which by default is a DataRowView when you're using a default SqlDataSource. Then get the value from the column in the DataRowView, see if it's not null and see if you can find it in the DropDownList. The following should work:
Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) _
          Handles DetailsView1.DataBound
    Select Case DetailsView1.CurrentMode
      Case DetailsViewMode.Edit
        Dim myDataRowView As DataRowView = DetailsView1.DataItem
        If myDataRowView("Description") IsNot DBNull.Value Then
          Dim myDropDownList As DropDownList = _ 
         CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
          If myDropDownList.Items.FindByValue(myDataRowView("Description")) _
         IsNot Nothing Then
            myDropDownList.Items.FindByValue(myDataRowView("Description")).Selected = True
          End If
        End If
    End Select
  End Sub
  There might be more efficient ways to do this though....
They say about .NET that it makes difficult things easy, and easy things difficult. I don't think that has changed much for .NET 2.0.... ;)

Hope this helps,

Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Here To Stay by KoRn (Track 1 from the album: Untouchables) What's This?
Reply With Quote