We help IT Professionals succeed at work.
Get Started

RowDataBound fill dropdownlist

pascalmartin
pascalmartin asked
on
180 Views
Last Modified: 2015-02-26
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
Comment
Watch Question
Top Expert 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE