Click to See Complete Forum and Search --> : ASP.NET: databind dropdownlist in template column


mattisimo
05-13-2004, 11:34 AM
Hi, i am trying unsuccessfully to include a databound dropdownlist in the footer template of a datagrid template column. I get the error:
Object reference not set to an instance of an object. on teh line where i try and specify the datasource of the ddl. The code is below:

FROM THE WEB PAGE
<asp:TemplateColumn HeaderText="Representing">
<ItemTemplate>
<%# databinder.eval(container, "DataItem.Department") %>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList id="ddlDept" runat="server" DataTextField="dep_name" DataValueField="dep_id" />
</FooterTemplate>
</asp:TemplateColumn>

FROM THE CODE BEHIND

Private Sub displayDeptReps()
dgDeptReps.DataSource = getDeptRepData()
dgDeptReps.DataBind()

Dim oDataFunc As New dataFunctions

Dim dtDepartments As DataTable
dtDepartments = (oDataFunc.noParameterQuery("stp_getDepartments")) 'returns a datatable containing the values for the ddl.


ddlDept.DataSource = New DataView(dtDepartments) 'I have also tried using the datatable as the source directly
ddlDept.DataTextField = "dep_name"
ddlDept.DataValueField = "dep_id"
ddlDept.DataBind()

End Sub

This works so long as the ddl is outside of the datagrid!
Could anyone point me in the right direction? It appears that the ddl is not created yet, perhaps...
Thanks,
Matt

mattisimo
05-14-2004, 06:32 AM
Solved it, for those who are interested, the solution is outlined in this article:

http://www.extremeexperts.com/Net/Articles/AddingControlstoFooter.aspx

You don't declare a footer template in the template column, then you create a dropdownlist object, databind it and then add it to the footer cell during the itemcreated event of the datagrid.

Hope this stops someone else getting a headache.

Matt