views:

31

answers:

0

Hi,

I have a treeview being populated from sql on a c# winform pulling the root node and its child ids using the following methods. From there i have a label in the TVCTOptions_AfterSelect statement of the treeview to capture that ID of the selected node. The problem i have is it only pulls root ids and not the childs id. Any ideas what i have missed?

Thanks

     private void populateRootResolutionLevelAll(bool ireset)
    {
        // Locals

        Functionality func = new Functionality();
        SqlConnection supportDB = null;
        DataTable t = new DataTable();
        bool opDeleted = false;

        string spName = "gssp_TechCallTrackerResolutionOptionSelectAllWithDelete";

        try
        {
            using (supportDB = new SqlConnection(getConnectionString(ConnectionType.GriffinSupportDB)))
            {
                using (SqlCommand getCallTrackerOptions = new SqlCommand(spName, supportDB))
                {
                    // Now set up the rest of the command object
                    getCallTrackerOptions.CommandType = CommandType.StoredProcedure;


                    // Populate the parameters.
                    getCallTrackerOptions.Parameters.Clear();
                    getCallTrackerOptions.Parameters.Add(func.CreateParameter("@optionDeleted", SqlDbType.Bit, ParameterDirection.Input, opDeleted));
                    getCallTrackerOptions.Parameters.Add(func.CreateParameter("@spErrorID", SqlDbType.Int, ParameterDirection.Output, DBNull.Value));

                    // Set up the dataset
                    supportDB.Open();
                    SqlDataAdapter da = new SqlDataAdapter(getCallTrackerOptions);
                    da.Fill(t);
                    supportDB.Close();

                    // Build TreeList Nodes.
                    foreach (DataRow r in t.Rows)
                    {
                        // add top level rows and then add their child rows on each itteration.
                        if (r["parentOptionID"].ToString() == "0")
                        {
                            TreeNode masterNode = new TreeNode(r["resolution"].ToString());
                            if (GetResolutionChildNodes(r["resolutionOptionID"].ToString(), t) != null)
                            {

                                List<TreeNode> childNodeList = GetResolutionChildNodes(r["resolutionOptionID"].ToString(), t);
                                masterNode.Nodes.AddRange(childNodeList.ToArray());

                            }

                            masterNode.Tag = "0";
                            masterNode.Name = r["resolution"].ToString();
                            TVResolution.Nodes.Add(masterNode);

                        }
                    }

                }
            }
        }

        catch (Exception e)
        {
            throw e;
        }

    }

    static List<TreeNode> GetResolutionChildNodes(string parentOptionID, DataTable t)
    {
        List<TreeNode> nodeList = new List<TreeNode>();
        foreach (DataRow r in t.Rows)
        {
            if (r["parentOptionID"].ToString() == parentOptionID)
            {
                // create child
                TreeNode node = new TreeNode(r["resolution"].ToString());
                node.Tag = parentOptionID.ToString();

                // check if this child has children.
                if (GetResolutionChildNodes(r["resolutionOptionID"].ToString(), t) != null)
                {
                    node.Nodes.AddRange(GetResolutionChildNodes(r["resolutionOptionID"].ToString(), t).ToArray());
                    node.Name = (r["resolutionOptionID"].ToString());
                }

                nodeList.Add(node);
            }
        }

        if (nodeList.Count != 0)
            return nodeList;
        else
            return null;    // returns null when no children were found.
    }

        private void TVCTOptions_AfterSelect(object sender, TreeViewEventArgs e)
    {

        //Get selected values ID and populate label
        LBCTOptionsValueMemberOutput.Text = "";
        LBCTOptionsValueMemberOutput.Text = TVCTOptions.SelectedNode.Name.ToString();

    }