递归以在datagridview c#winforms中显示父子值

时间:2016-04-08 08:33:07

标签: c# winforms

使用递归我想在c#winforms中显示数据网格视图的内容列表。我在下面尝试了但是在datagridview中只显示没有显示子值的父值

    public class PageItem
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string MenuText { get; set; }
        public List<PageItem> Childs { get; set; }
    }

条件: -

    public List<PageItem> GetPageItems()
    {
        List<PageItem> pageItems = new List<PageItem>();
        SqlCeConnection conn = new SqlCeConnection(@"Data                                                                        
       Source=D:\database\Employee.mdf;");
        SqlCeCommand cmd = new SqlCeCommand("SELECT Id, ParentId, MenuTitle 
        FROM Page", conn);
        conn.Open();
        SqlCeDataReader rdr = cmd.ExecuteReader();

         var allItems = new List<PageItem>();


        while (rdr.Read())
        {
           var item = (new PageItem()
                      {
                          Id = Convert.ToInt32(rdr["Id"]),
                          ParentId = Convert.ToInt32(rdr["ParentId"]),
                          MenuText = rdr["MenuTitle"].ToString()

                      });

           allItems.Add(item);
           var parent = allItems.Where(pi => pi.Id == item.ParentId).SingleOrDefault();

           if (parent == null)
           {
              pageItems.Add(item);


           }
           else
           {
               if (parent.Childs == null)
                   parent.Childs = new List<PageItem>();
               parent.Childs.Add(item);


           }
        }

        rdr.Close();
        conn.Close();

        return pageItems;
    }

表单加载: -

    private void Form1_Load(object sender, EventArgs e)
    {
        GetPageItems();
        this.dataGridView1.DataSource = GetPageItems();

        this.comboBox1.DataSource = GetPageItems();
        this.comboBox1.DisplayMember = "MenuText";
        this.comboBox1.ValueMember = "ParentId";
    }

从上面的代码我得到了这样的输出: -

     parent 0
     parent 1
     parent 2
     parent 3

我需要像这样的输出: -

    Parent 0
      Child 1
      Child 2
      Child 3
          Child 3.1
          Child 3.2
          Child 3.3
    Parent 1
    Parent 2
    Parent 3

谢谢..

1 个答案:

答案 0 :(得分:1)

最后我得到了使用递归的上述问题的答案。如果有人需要使用它谢谢你: -

    public class Student
    {
        public int ID { get; set; }
        public int ParentID { get; set; }
        public string Title { get; set; }
    }

    public class Parent
    {
        public int pID { get; set; }
        public int pParentID { get; set; }
        public string pTitle { get; set; }
    }

    public class OutPut
    {
        public int mID { get; set; }
        public int mParentID { get; set; }
        public string mTitle { get; set; }
    }

    public class SubOutPut
    {
       public int SubsmID { get; set; }
        public int SubsmParentID { get; set; }
        public string SubsmTitle { get; set; }

    }

表单加载

    public static List<SubOutPut> ChildList = new List<SubOutPut>();

    private void Form3_Load(object sender, EventArgs e)
    {
        List<Student> std = loaddataFull();
        List<Parent> prnt = loaddataParent();
        List<OutPut> MenuItems = new List<OutPut>();

        foreach (Parent id in prnt)
        {

            int pid=Convert.ToInt32(id.pID);

            //Adding Parent Values to the List
            List<SubOutPut> SubMenuItems = new List<SubOutPut>();

            MenuItems.Add(new OutPut()
            {
                mID=Convert.ToInt32(id.pID),
                mParentID=Convert.ToInt32(id.pParentID),
                mTitle=Convert.ToString(id.pTitle),
            });
         SubMenuItems = GetChildrens(pid);
            foreach (SubOutPut Add in SubMenuItems)
                {
                    MenuItems.Add(new OutPut()
                    {
                        mID = Convert.ToInt32(Add.SubsmID),
                        mParentID = Convert.ToInt32(Add.SubsmParentID),
                        mTitle = Convert.ToString(Add.SubsmTitle)
                    });
           }
            SubMenuItems.Clear();
        }

        dataGridView1.DataSource = MenuItems;
        foreach (var item in MenuItems)
        {
            listView1.Items.Add(item.mID.ToString());
            listView1.Items.Add(item.mParentID.ToString());
            listView1.Items.Add(item.mTitle.ToString());
            listView1.View = View.Tile;

        }

        dataGridView2.DataSource = loaddataParent();
    }

从数据库加载所有数据

    public List<Student> loaddataFull()
    {
        List<Student> student = new List<Student>();
        SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=test;Integrated Security=true");
        SqlCommand cmd = new SqlCommand("select * from testing", conn);
        SqlDataReader dr;
        try
        {
            conn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                student.Add(new Student()
                {
                    ID = dr.GetInt32(dr.GetOrdinal("id")),
                    ParentID = dr.GetInt32(dr.GetOrdinal("pid")),
                    Title = dr.GetString(dr.GetOrdinal("title"))
                });

            }
            dr.Close();
        }
        catch (Exception exp)
        {

            throw;
        }
        finally
        {

            conn.Close();
        }

        return student;
    }

仅加载父值: -

    public List<Parent> loaddataParent()
    {
        List<Parent> parent = new List<Parent>();
        SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=test;Integrated Security=true");
        SqlCommand cmd = new SqlCommand("select * from testing where pid=0", conn);
        SqlDataReader dr;
        try
        {
            conn.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                parent.Add(new Parent()
                {
                    pID = dr.GetInt32(dr.GetOrdinal("id")),
                    pParentID = dr.GetInt32(dr.GetOrdinal("pid")),
                    pTitle = dr.GetString(dr.GetOrdinal("title"))
                });

            }

            dr.Close();
        }
        catch (Exception exp)
        {

            throw;
        }
        finally
        {

            conn.Close();
        }

        return parent;
    }

这里有递归方法: -

    public string gg = "         ";
    public List<SubOutPut> GetChildrens(int ID)
    {

        List<Student> std = loaddataFull();

        foreach (Student Child in std)
        {
            if (Child.ParentID == ID)
            {
                ChildList.Add(new SubOutPut()
                {
                    SubsmID = Convert.ToInt32(Child.ID),
                    SubsmParentID = Convert.ToInt32(Child.ParentID),
                    SubsmTitle = Convert.ToString(gg + Child.Title)
                });

                gg = gg+gg;
                GetChildrens(Child.ID);
                gg = "       ";
            }
        }
        return ChildList;
    }

谢谢:---