pascalmartin
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="DriverTextC hanged" />
</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("d dlDriver") , DropDownList)
Dim myConn As SqlConnection = New SqlConnection(Configuratio nManager.C onnectionS trings("Lo calSqlServ er").Conne ctionStrin g)
Dim myCommand1 As SqlCommand = New SqlCommand("GetDriver", myConn)
myCommand1.CommandText = "GetDriver"
myCommand1.CommandType = CommandType.StoredProcedur e
Using myConn
myConn.Open()
Dim DRDriver As SqlDataReader = myCommand1.ExecuteReader(C ommandBeha vior.Close Connection )
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("d dlReplace" ), DropDownList)
Dim myConn2 As SqlConnection = New SqlConnection(Configuratio nManager.C onnectionS trings("Lo calSqlServ er").Conne ctionStrin g)
Dim myCommand2 As SqlCommand = New SqlCommand("GetDriver", myConn2)
myCommand2.CommandText = "GetDriver"
myCommand2.CommandType = CommandType.StoredProcedur e
Using myConn2
myConn2.Open()
Dim DRDriver1 As SqlDataReader = myCommand2.ExecuteReader(C ommandBeha vior.Close Connection )
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
Here below my code:
<asp:TemplateField HeaderText = "Driver" >
<ItemTemplate>
<asp:DropDownList id="ddlDriver" runat="server" AutoPostBack="true" OnTextChanged="DriverTextC
</ItemTemplate>
<EditItemTemplate>
<%--<asp:DropDownList ID="ddlReplace" runat="server" />
--%></EditItemTemplate>
</asp:TemplateField>
In the code behind:
Sub GridDelivery_RowDataBound(
If GridDelivery.EditIndex = -1 Then 'GRIDVIEW NOT IN EDIT MODE
If (e.Row.RowType = DataControlRowType.DataRow
Dim ddlDriver As DropDownList = CType(e.Row.FindControl("d
Dim myConn As SqlConnection = New SqlConnection(Configuratio
Dim myCommand1 As SqlCommand = New SqlCommand("GetDriver", myConn)
myCommand1.CommandText = "GetDriver"
myCommand1.CommandType = CommandType.StoredProcedur
Using myConn
myConn.Open()
Dim DRDriver As SqlDataReader = myCommand1.ExecuteReader(C
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
Dim ddlDriver1 As DropDownList = CType(e.Row.FindControl("d
Dim myConn2 As SqlConnection = New SqlConnection(Configuratio
Dim myCommand2 As SqlCommand = New SqlCommand("GetDriver", myConn2)
myCommand2.CommandText = "GetDriver"
myCommand2.CommandType = CommandType.StoredProcedur
Using myConn2
myConn2.Open()
Dim DRDriver1 As SqlDataReader = myCommand2.ExecuteReader(C
ddlDriver1.DataSource = DRDriver1
ddlDriver1.DataValueField = "PlateNB"
ddlDriver1.DataTextField = "DriverID"
ddlDriver1.DataBind()
ddlDriver1.Items.Insert(0,
End Using
End If
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent!
Kindly try this also if not resolved yet. Change your code from
Open in new window
to
Open in new window
Hope this helps you.
Regards,
Kaushal Arora