Link to home
Start Free TrialLog in
Avatar of pascalmartin
pascalmartinFlag for Hong Kong

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of pascalmartin

ASKER

Excellent!