In the OnRowDataBoundevent
of the ASP.Net GridView I am trying to populate the Department DropDownList (ddlDepts) which will be displayed when a row is edited.
The code below is not working as the DropdownList is empty.
Any ideas what I am doing wrong?
'//GridView markup:
<asp:TemplateField HeaderText="Department" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID = "lblDept" runat="server" Text='<%# Eval("deptName")%>'></asp:Label>
<asp:DropDownList ID="ddlDepts" runat="server" Visible = "false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
''//Codefile
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cmd As New SqlCommand("SELECT DISTINCT(DeptID,DeptName) FROM Departments")
Dim ddlDepts As DropDownList = TryCast(e.Row.FindControl("ddlDepts"), DropDownList)
ddlDepts.DataSource = Me.ExecuteQuery(cmd, "SELECT")
ddlDepts.DataTextField = "DeptID"
ddlDepts.DataValueField = "DeptName"
ddlDepts.DataBind()
Dim DeptName As ListItem = ddlDepts.Items.FindByValue("lblDept")
If DeptName IsNot Nothing Then
ddlDepts.SelectedValue = DeptName.Value
End If
End If
End Sub