Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to implement a cascading dropdownlist in a gridview footer template. My code is given below. I am getting an error message "Object reference not set to an instance of an object."

C#
public void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer) {
            DropDownList ddlGrandParent = (DropDownList)e.Row.FindControl("ddlGrandParentService");

                ddlGrandParent.DataSource = GetGrandParentService();
                ddlGrandParent.DataValueField = "GrandParentServiceID";
                ddlGrandParent.DataTextField = "GrandService";
                ddlGrandParent.DataBind();

                //ddlGrandParent.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

                DropDownList ddlParent = (DropDownList)e.Row.FindControl("ddlParentService");

          //**Getting error "Object reference not set to an instance of an object."**

           ddlParent.DataSource = GetParentServiceByGrandParent  (Convert.ToInt32((GridView1.FooterRow.FindControl
        ("ddlGrandParentService") as DropDownList).SelectedItem.Value));


                ddlParent.DataValueField = "ParentServiceId";
                ddlParent.DataTextField = "ParentServiceName";
                ddlParent.DataBind();

}
}


private List<GrandParentService> GetGrandParentService()
    {

        List<GrandParentService> all = new List<GrandParentService>();
        using (ABCEntities dc = new ABCEntities())
        {
            all = dc.GrandParentService.ToList();
        }
        return all;
    }


private List<ParentService> GetParentServiceByGrandParent(int grandParentID)
    {

        List<ParentService> all = new List<ParentService>();
        using (ABCEntities dc = new ABCEntities())
        {
            all = dc.ParentService.Where(a => a.GrandParentServiceID.Equals(grandParentID)).ToList();
        }
        return all;

    }



Please help me. Thanks in advance.
Posted
Comments
Amey K Bhatkar 30-Dec-13 13:57pm    
Create hidden field and store the value of dropdownlist selection inside that and use that hidden filed value as the drop down selected item.
JoCodes 30-Dec-13 14:28pm    
obviously FindControl("ddlParentService") not successful

1 solution

bind data to gridview in GridView1_RowCreated event using user defined function then use
C#
DropDownList ddlParent = (DropDownList)GridView1.Rows[e.NewIndex].FindControl("ddlParentService");

It will automatically assign dropdownlist ddlparentservice to ddlparent object then assign datasource to ddlparent object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900