Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

populating a treeview (winforms, c#)

$
0
0

Hello.  Have a treeview that does populate with the following code that is posted below.  But this only shows the top most level (Project) and one child level below Project.  The information for the treeview is coming from sql server view.  But if I change the info so it comes from 2 tables (tblProject and tblToDoItem) I get the same structure of top level Project and one child level below.  The test data has a ToDoItem that would be a child of a child but it still shows in the treeview at the first child level. 

Can someone review this information below and see where or what is wrong with what there is because I'm not seeing it. 

Thanks...John

        string projectSQL = "SELECT DISTINCT ProjectID, ProjectName FROM ToDoItem_vw";
        string todoitemSQL = "SELECT ToDoItemID, ToDoItem, ProjectID, ProjectName, ParentToDoItemID FROM ToDoItem_vw";
        private void FillingTreeview()
        {
            command = new SqlCommand(projectSQL, conn);
            adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            conn.Open();
            adapter.Fill(ds, "Project");
            adapter.SelectCommand.CommandText = todoitemSQL;
            adapter.Fill(ds, "ToDoItem");
            conn.Close();
            DataColumn parentCol = ds.Tables["Project"].Columns["ProjectID"];
            DataColumn childCol = ds.Tables["ToDoItem"].Columns["ProjectID"];
            DataRelation relation = new DataRelation("ProjectID", parentCol, childCol);
            ds.Relations.Add(relation);
            foreach (DataRow parent in ds.Tables["Project"].Rows)
            {
                TreeNode nodeParent = tvwToDoItem.Nodes.Add(parent["ProjectName"].ToString());
                foreach (DataRow child in parent.GetChildRows(relation))
                {
                    nodeParent.Nodes.Add(child["ToDoItem"].ToString());
                }
            }
            tvwToDoItem.ExpandAll();
        }


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>