Click here to Skip to main content
15,894,097 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi i have the following codes running in my project. What i want to know from you guys is that how do i write the same code in EF 4 to populate a treeView. I have one table named "tPatients" from where i am displaying all patient names datewise. i.e.

18-June-2013
--Mr. A
--Mr. B
19-June-2013
--Mr. C
--Mr. D
--Mr. E
20-June-2013
--Mr. F
--Mr. G



Hope you guys help me. Thanking you.
C#
private void PopulateTreeView()
    {
        treePatient.Nodes.Clear();

        SqlDataAdapter daPatient = new SqlDataAdapter("SELECT TOP 100 PERCENT pId, pBillDate, pName FROM tPatients WHERE pBillDate >= DATEADD(day,-7, GETDATE())", con);
        SqlDataAdapter daDate = new SqlDataAdapter("SELECT * FROM viewGroupDate", con);

        DataSet ds = new DataSet();
        daPatient.Fill(ds, "tPatients");
        daDate.Fill(ds, "viewGroupDate");

        //Add root node
        TreeNode root = new TreeNode("Patients");
        root.Tag =0;
        root.Expand();

            ds.Relations.Add("Dept_SubDept", ds.Tables["viewGroupDate"].Columns["pBillDate"], ds.Tables["tPatients"].Columns["pBillDate"]);
            foreach (DataRow dr in ds.Tables["viewGroupDate"].Rows)
            {

                DateTime dt = Convert.ToDateTime(dr["pBillDate"]);
                TreeNode tn = new TreeNode(String.Format("{0:dd-MMM-yyyy}", dt));
                tn.Tag = 0;

                foreach (DataRow drChild in dr.GetChildRows("Dept_SubDept"))
                {

                    TreeNode childTn = new TreeNode(drChild["pId"].ToString() + "- " + drChild["pName"].ToString());
                    childTn.Tag = drChild["pId"];

                    tn.Nodes.Add(childTn);

                }

                root.Nodes.Add(tn);

            }
            treePatient.BeginUpdate();
            treePatient.Nodes.Add(root);  
            treePatient.EndUpdate();

    }
Posted

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