0

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.

4
  • On which event you are trying to get the Dropdown ?
    – Pushpendra
    Sep 18, 2013 at 13:34
  • I just ran your code, it works fine.. @Pushpendra asked the right question, on which event are you calling this code? Sep 18, 2013 at 13:47
  • Yes..its important to know the event as theoretically the code seems to me also correct and should ideally work in RowDataBound event.
    – Pushpendra
    Sep 18, 2013 at 14:52
  • The DataGridView Control easily bind their application to any data sources without any code. It is also able to bind any XML, custom business object and web service directly to the Data Grid control in Windows Forms development environments.
    – user20108
    Oct 14, 2013 at 10:19

1 Answer 1

0

I was trying to populate the dropdown before I had done a databind on the gridview. I called a sub to populate the drop down (shown above) and then another call to populate the gridview. When I switched these calls around it all ran fine. I believe this is because the gridview wasn't actually there hence the null reference when I tried to initialise row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.