Stack Exchange
RowDataBound DropDownList
I have a RowDataBound event handler that looks like this:    
Public Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GVHistoricNames.RowDataBound 'RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim hyperlinkUSNHyperlink As HyperLink = CType(e.Row.FindControl("USNHyperlink"), HyperLink)
        Dim ddl As DropDownList = CType(e.Row.FindControl("ddlUsercode"), DropDownList)
        If ddl.SelectedValue = "" Then 'labLastUserCode.Text = "" Then
            hyperlinkUSNHyperlink.NavigateUrl = ""
        End If
    End If
 End Sub

...and a RowCreated event handler that looks like this:
Public Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GVHistoricNames.RowCreated 'RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ddl As DropDownList = CType(e.Row.FindControl("ddlUsercode"), DropDownList)
        ddl.Items.Add("")
        ddl.Items.Add(strUserName)
    End If
End Sub

...and a RowUpdating event handler that looks like this:
Protected Sub GVHistoricNames_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GVClearcoreHistoricNames.RowUpdating
    Try
        Dim ddl As DropDownList = CType(GVHistoricNames.Rows(e.RowIndex).FindControl("ddlUsercode"), DropDownList)
        SQLHistoricNames.UpdateParameters("UserCode").DefaultValue = ddl.SelectedValue

    Catch ex As Exception

    Finally

    End Try
End Sub

Please see line three of the RowUpdating event handler. The value of the SelectedValue property is never correct because the RowDataBound event handler is called after the RowUpdating event handler. How do I access SelectedValue? I want to set it as an update parameter.

One of the way could be to look into the actual request data. For example, in GVHistoricNames_RowUpdating code, use
Dim ddl As DropDownList = CType(GVHistoricNames.Rows(e.RowIndex).FindControl("ddlUsercode"), DropDownList)
SQLHistoricNames.UpdateParameters("UserCode").DefaultValue = Request(ddl.UiniqueID)

I often use such work-arounds when the control value is needed before post data could be loaded into control (or when controls are added/bound dynamically at a later event).
EDIT 
ASP.NET uses Control.UniqueId to represent name property of corresponding html element. It (as well as ClientID) typically gets constructed by appending control's id to parent's (parent that is naming container) unique id, hence you get different unique ids (and client ids) for multiple drop-down lists in the grid (because each row acts as a naming container)  
As far as your problem goes, you are probably creating drop-down list in design time template while you are loading your list items in row created. However, before row-created event is fired, the drop-down list would have been already added to page control tree and its POST events would have been already processed. In such case, there would be no items in the drop-down list at that time to set the selection. Hence the issue. 

What is ddl.UIniqueID? There is a dropdownlist for every row in the GridView.

Many thanks.  That has done the trick.  I wish I could upvote this answer more than once! Can you explain why the behavior above does not happen if I declare the drop down list items in the ASPX file instead of the code behind?

@w0051977, please see my edit in the answer for your doubts! BTW, instead of up-voting twice, you can mark the answer as accepted by clicking the tick below the voting arrows.

One more thing before I mark the answer.  if I declare the drop down items in the ASPX page at design time then I do not have this problem.  If the items are declared at design time, then does this mean that the drop down list is populated before the RowUpdating event?

@w0051977, if you can supply list items at the design time then whenever, the control created, items are added to it and then it is added to the page control tree. View-state loading and post data processing happens whenever control is added to control tree (applicable for controls that are added to control tree after/at page load) and selection is set at this stage. It does not have anything to do with timing of RowUpdating event but rather it depends if drop-down list items are available to set the section when post data for the control is processed.
