I have a gridview which contains a dropdownlist in the header row to allow users to filter the data:
<asp:GridView ID="GV_Projects" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" DataKeyNames="Title"
OnRowDataBound="OnRowDataBound" EnableModelValidation="True" Style="width: 95%;
float: left;">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField ItemStyle-Width="150px" DataField="Developer" HeaderText="Developer" />
<asp:TemplateField>
<HeaderTemplate>
Division<br />
<asp:DropDownList ID="DDL_DivisionFilter" runat="server" AutoPostBack="True">
<asp:ListItem>All</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Division") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
Within my code I attempt to populate this dynamically based on results from a query:
GridViewRow row = GV_Projects.HeaderRow;
DropDownList DDL_Division = new DropDownList();
DDL_Division = (DropDownList)row.FindControl("DDL_DivisionFilter");
make connection, query add each result to drop down list
however everytime it gets to "(dropdownlist)row.FindControl("DDL_DivisionFilter") it errors out as a "NullReferenceException" I just can't understand why the variable row is null, I have only recently switched from vb to c# so am not quite there with fixing this.