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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.