Johnny Bones
Johnny Bones

Reputation: 8404

DropDownList inside a GridView is not being seen as an object

I can't figure out what I'm doing wrong here. Trying to dynamically fill a dropdownlist inside a gridview and it keeps telling me the object reference is not set to an instance of an object.

First, my ASPX:

<asp:Panel runat="server" ID="ShowDiv3" Visible="false" BorderStyle="Solid" BorderWidth="0"  Width="1389px">
        <asp:Label ID="lblShowDiv3Title" runat="server" Text="Root Causes:    " Font-Bold="true"></asp:Label><asp:DropDownList ID="ddlRootCauses" runat="server" Width="300px" OnSelectedIndexChanged="ddlRootCauses_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
        <br />
        <%-- This line needed to be commented out when paging was removed: AllowPaging="True"  AllowCustomPaging="True" PageSize="10" --%>
        <div id="divGrid3" style='width:1290px; overflow:auto'>
    <asp:GridView ID="DataGrid_RootCauses" runat="server" 
        AllowSorting="True" OnSorting="DataGrid_RootCauses_Sorting" AutoGenerateColumns="False" ShowFooter ="true" CellPadding="1" 
        CssClass="hoverTable"
        HeaderStyle-BackColor="#4DA6A6" HeaderStyle-BorderColor="#4DA6A6" 
        HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="White"
        FooterStyle-BackColor="#4DA6A6" FooterStyle-BorderColor="#4DA6A6" FooterStyle-ForeColor="White"
        DataKeyNames="root_cause_id"
                        OnRowDataBound="DataGrid_RootCauses_RowDataBound"
        OnRowCancelingEdit="DataGrid_RootCauses_CancelCommand"   
        OnRowEditing="DataGrid_RootCauses_EditCommand" 
        OnRowDeleting="DataGrid_RootCauses_DeleteCommand"
        OnRowUpdating="DataGrid_RootCauses_UpdateCommand">  
            <Columns>  

            <asp:TemplateField HeaderText="LPI Due">  
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_LPI_DUE" runat="server" Width="70px" Text='<%#Eval("LPI_DUE") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_LPI_DUE" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>                    

        <asp:TemplateField HeaderText="Root Cause Category">  
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_LPI_Category" runat="server" Width="170px" Text='<%#Eval("LPI_Category") %>'></asp:DropDownList>
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_LPI_Category" runat="server" Width="170px" ></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>                   

        <asp:TemplateField HeaderText="Root Cause Reason">  
            <ItemTemplate>  
                <asp:Label ID="lbl_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:TextBox ID="txt_RC_LPI_Reason" runat="server" Width="370px" Text='<%#Eval("LPI_reason") %>'></asp:TextBox>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:TextBox ID="ntxt_RC_LPI_Reason" runat="server" Width="370px" ></asp:TextBox>  
            </FooterTemplate>
        </asp:TemplateField>                 

        <asp:TemplateField HeaderText="Business Owned">  
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Business" runat="server" Width="70px" Text='<%#Eval("Business") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Business" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>   

        <asp:TemplateField HeaderText="CP Owned">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_CP" runat="server" Width="70px" Text='<%#Eval("CP") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_CP" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField>                    

        <asp:TemplateField HeaderText="Non-CP Owned">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Non_CP" runat="server" Width="70px" Text='<%#Eval("Non_CP") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Non_CP" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Proclaim">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Proclaim" runat="server" Width="70px" Text='<%#Eval("Proclaim") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Proclaim" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="PMHS">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_PMHS" runat="server" Width="70px" Text='<%#Eval("PMHS") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_PMHS" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Facets">  
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>  
                <asp:Label ID="lbl_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'></asp:Label>  
            </ItemTemplate>  
            <EditItemTemplate>  
                <asp:DropDownList ID="ddl_RC_Facets" runat="server" Width="70px" Text='<%#Eval("Facets") %>'>
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </EditItemTemplate>  
            <FooterTemplate>  
                <asp:DropDownList ID="nddl_RC_Facets" runat="server" Width="70px" >
                    <asp:ListItem Value=""> - </asp:ListItem>
                    <asp:ListItem Value="N"> N </asp:ListItem>
                    <asp:ListItem Value="Y"> Y </asp:ListItem>
                </asp:DropDownList>  
            </FooterTemplate>
        </asp:TemplateField> 

        <asp:TemplateField ItemStyle-Width="90px">  
        <ItemTemplate>  
            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
        </ItemTemplate>  
        <EditItemTemplate>  
            <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
            <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
        </EditItemTemplate>  
        <FooterTemplate>
            <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="DataGrid_RootCauses_RowCreated" />
        </FooterTemplate>
        </asp:TemplateField>  
        <asp:TemplateField>  
        <ItemTemplate>  
            <asp:Button ID="btn_Delete" runat="server" OnClientClick="javascript:return confirm('Are you sure?');" Text="Delete" CommandName="Delete" />  
        </ItemTemplate>  
        </asp:TemplateField>        
        <asp:TemplateField HeaderText="Cause ID">  
        <ItemTemplate>  
            <asp:Label ID="lbl_Cause_ID" runat="server" Text='<%#Eval("root_cause_id") %>'></asp:Label>  
        </ItemTemplate>  
        </asp:TemplateField>        

        </Columns>  
    </asp:GridView>  
    <asp:Label ID="lblEmpty1" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label>
    </div> 
</asp:Panel>

Now, the code-behind:

protected void  ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadRootCauseGrid();

    OracleConnection conn = GetConnection();
    try
    {
        {
            // ddlRootCauses
            OracleCommand cmd6 = new OracleCommand();
            cmd6.CommandType = CommandType.StoredProcedure;
            cmd6.CommandText = "SP_LPI_MAINT_LIST_ROOTCAUSE";
            cmd6.Connection = conn;

            //cmd3.Parameters.Add("v_Lookup_Value", OracleType.VarChar, 20).Value = "LEAD";
            cmd6.Parameters.Add("vRootCauseList", OracleType.Cursor).Direction = ParameterDirection.Output;

            var SearchAdapter6 = new OracleDataAdapter(cmd6);
            var ds6 = new DataTable();
            SearchAdapter6.Fill(ds6);

            DataView view = new DataView(ds6);
            DataTable distinctValues = new DataTable();
            distinctValues = view.ToTable(true, "LPI_Category");

            // Fill all of the dropdowns from the same data adapter
            {
                //This one works fine
                ddlRootCauses.DataSource = distinctValues;
                ddlRootCauses.DataTextField = "LPI_Category";
                ddlRootCauses.DataValueField = "LPI_Category";
                ddlRootCauses.DataBind();
                ddlRootCauses.Items.Insert(0, new ListItem("All", ""));

                //This one send me down to the Catch block on the first line
                ddl_RC_LPI_Category.DataSource = distinctValues;
                ddl_RC_LPI_Category.DataTextField = "LPI_Category";
                ddl_RC_LPI_Category.DataValueField = "LPI_Category";
                ddl_RC_LPI_Category.DataBind();
                ddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", ""));

                nddl_RC_LPI_Category.DataSource = distinctValues;
                nddl_RC_LPI_Category.DataTextField = "LPI_Category";
                nddl_RC_LPI_Category.DataValueField = "LPI_Category";
                nddl_RC_LPI_Category.DataBind();
                nddl_RC_LPI_Category.Items.Insert(0, new ListItem("All", ""));

            }

            ds6.Dispose();
            cmd6.Dispose();
            SearchAdapter6.Dispose();

            conn.Close();
        }
    }
    catch (Exception ex)
    {
            Response.Write(ex.Message);
            conn.Close();
    }

}

Oddly enough, ddlRootCauses gets populated with no problems. But, that dropdownlist is outside of the gridview. I don't understand why it's having trouble with the dropdownlist inside the gridview. And, it can't just be because gridviews don't like dropdowns; the Y/N ones with hardcoded listitems work fine.

Upvotes: 0

Views: 45

Answers (1)

VDWWD
VDWWD

Reputation: 35544

Since the controls are inside a GridView, you cannot access them like you would a Contol that is outside of it (like ddlRootCauses)

To populate a DropDownList inside a GridView you need to find them with FindControl first.

protected void ddlRootCauses_SelectedIndexChanged(object sender, EventArgs e)
{
    //find the dropdownlist in the footer row
    DropDownList ddl = DataGrid_RootCauses.FooterRow.FindControl("nddl_RC_LPI_Category") as DropDownList;

    //or in a normal row you need to use an index
    DropDownList ddl = DataGrid_RootCauses.Rows[5].FindControl("ddl_RC_LPI_Category") as DropDownList;

    ddl.DataSource = distinctValues;
    ddl.DataTextField = "LPI_Category";
    ddl.DataValueField = "LPI_Category";
    ddl.DataBind();
    ddl.Items.Insert(0, new ListItem("All", ""));
}

Example with RowDataBound

First the aspx

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="true">
    <Columns>
    <asp:TemplateField HeaderText="Root Cause Reason">  
        <ItemTemplate>  
            <asp:DropDownList ID="DropDownListItem" runat="server"></asp:DropDownList>
        </ItemTemplate>  
        <EditItemTemplate>  
            <asp:DropDownList ID="DropDownListEditItem" runat="server"></asp:DropDownList> 
        </EditItemTemplate>  
        <FooterTemplate>  
            <asp:DropDownList ID="DropDownListFooter" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>  
    </Columns>
</asp:GridView>

And the code behind

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //the header row
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //normal row
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //normal row in edit mode
            DropDownList ddl = e.Row.FindControl("DropDownListEditItem") as DropDownList;

            ddl.DataSource = distinctValues;
            ddl.DataTextField = "myValue";
            ddl.DataValueField = "ID";
            ddl.DataBind();
        }
        else
        {
            //normal row
            DropDownList ddl = e.Row.FindControl("DropDownListItem") as DropDownList;

            ddl.DataSource = distinctValues;
            ddl.DataTextField = "myValue";
            ddl.DataValueField = "ID";
            ddl.DataBind();
        }
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //footer row
        DropDownList ddl = e.Row.FindControl("DropDownListFooter") as DropDownList;

        ddl.DataSource = distinctValues;
        ddl.DataTextField = "myValue";
        ddl.DataValueField = "ID";
        ddl.DataBind();
    }
}

Or in a button click.

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = row.FindControl("DropDownListItem") as DropDownList;

            ddl.DataSource = Common.LoadFromDB();
            ddl.DataTextField = "field01";
            ddl.DataValueField = "itemID";
            ddl.DataBind();
        }
    }
}

Upvotes: 0

Related Questions