RowDataBound fill dropdownlist

I can't bind my dropdownlist when my gridview goes on edit mode. I need help here.

Here below my code:

<asp:TemplateField HeaderText = "Driver" >
<ItemTemplate>
<asp:DropDownList id="ddlDriver" runat="server" AutoPostBack="true" OnTextChanged="DriverTextChanged" />
</ItemTemplate>
<EditItemTemplate>
<%--<asp:DropDownList ID="ddlReplace" runat="server"  />
--%></EditItemTemplate>
</asp:TemplateField>

In the code behind:

Sub GridDelivery_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If GridDelivery.EditIndex = -1 Then 'GRIDVIEW NOT IN EDIT MODE
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim ddlDriver As DropDownList = CType(e.Row.FindControl("ddlDriver"), DropDownList)
                Dim myConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
                Dim myCommand1 As SqlCommand = New SqlCommand("GetDriver", myConn)
                myCommand1.CommandText = "GetDriver"
                myCommand1.CommandType = CommandType.StoredProcedure
                Using myConn
                    myConn.Open()
                    Dim DRDriver As SqlDataReader = myCommand1.ExecuteReader(CommandBehavior.CloseConnection)

                    ddlDriver.DataSource = DRDriver
                    ddlDriver.DataValueField = "PlateNB"
                    ddlDriver.DataTextField = "DriverID"
                    ddlDriver.DataBind()
                    ddlDriver.Items.Insert(0, "[Select a Driver]")
                End Using
            End If
        End If

        If GridDelivery.EditIndex = 1 Then 'GRIDVIEW NOT IN EDIT MODE
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim ddlDriver1 As DropDownList = CType(e.Row.FindControl("ddlReplace"), DropDownList)
                Dim myConn2 As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
                Dim myCommand2 As SqlCommand = New SqlCommand("GetDriver", myConn2)
                myCommand2.CommandText = "GetDriver"
                myCommand2.CommandType = CommandType.StoredProcedure
                Using myConn2
                    myConn2.Open()
                    Dim DRDriver1 As SqlDataReader = myCommand2.ExecuteReader(CommandBehavior.CloseConnection)
                    ddlDriver1.DataSource = DRDriver1
                    ddlDriver1.DataValueField = "PlateNB"
                    ddlDriver1.DataTextField = "DriverID"
                    ddlDriver1.DataBind()
                    ddlDriver1.Items.Insert(0, "[Select a Driver]")
                End Using
            End If
        End If
    End Sub
pascalmartinAsked:
Who is Participating?
 
sammySeltzerCommented:
Hi

You didn't show the code for the update statement but anyhow, try this.

On the markup:

        <asp:TemplateField HeaderText = "Driver">
        <ItemTemplate>
          <asp:Label ID="lblDriver" runat="server" Text='<%# Eval("PlateNB")%>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
                <asp:Label ID="lblDriver" runat="server" Text='<%# Eval("DriverID")%>' Visible = "false"></asp:Label>
        <asp:DropDownList ID = "ddlDriver" runat = "server">
        </asp:DropDownList>
        </EditItemTemplate>

Open in new window


'Then your rowDataBound sub

    Sub GridDelivery_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow AndAlso GridDelivery.EditIndex = e.Row.RowIndex Then
                     Dim ddlDriver As DropDownList = CType(e.Row.FindControl("ddlDriver"), DropDownList)
                     Dim myConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)
                     Dim myCommand1 As SqlCommand = New SqlCommand("GetDriver", myConn)
                     myCommand1.CommandText = "GetDriver"
                     myCommand1.CommandType = CommandType.StoredProcedure
                     Using myConn
                         myConn.Open()
                         Dim DRDriver As SqlDataReader = myCommand1.ExecuteReader(CommandBehavior.CloseConnection)
    
                         ddlDriver.DataSource = DRDriver
                         ddlDriver.DataValueField = "PlateNB"
                         ddlDriver.DataTextField = "DriverID"
                         ddlDriver.DataBind()
                         'ddlDriver.Items.Insert(0, "[Select a Driver]")
                         ddlDriver.Items.FindByValue(TryCast(e.Row.FindControl("lblDriver"), Label).Text).Selected = True
                     End Using
                 End If

Open in new window


Now, depending on how your UPDATE statement is written, this should work or get you very close to what you are after.
0
 
Kaushal AroraTechnical AnalystCommented:
Dear pascalmartin,

Kindly try this also if not resolved yet. Change your code from

 If GridDelivery.EditIndex = 1 Then 'GRIDVIEW NOT IN EDIT MODE

Open in new window


to  

If GridDelivery.EditIndex != -1 Then 'GRIDVIEW NOT IN EDIT MODE

Open in new window


Hope this helps you.

Regards,
Kaushal Arora
0
 
pascalmartinAuthor Commented:
Excellent!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.