C# 在datagridview WinC窗体中显示父子值的递归

C# 在datagridview WinC窗体中显示父子值的递归,c#,winforms,C#,Winforms,使用递归,我希望在c winforms中显示datagridview的内容列表。我尝试了以下操作,但结果是datagridview只显示父值,不显示子值 public class PageItem { public int Id { get; set; } public int ParentId { get; set; } public string MenuText { get; set; } public Lis

使用递归,我希望在c winforms中显示datagridview的内容列表。我尝试了以下操作,但结果是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; }
    }
从上面的代码中,我得到如下输出:-

     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
我需要这样的输出:-

     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

谢谢..

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

    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;
    }
下面是递归方法:-


谢谢:--

您是否正在使用任何第三方框架,如devexpress?不,我没有使用..谢谢..您可能需要一些TreeView,而不是dataGridView。请看这里-winforms DataGridView无法执行我认为您要求它执行的操作,请看这些:,,-这些满足您的需要吗?好的,先生,谢谢您的宝贵时间…现在我将尝试使用treeview控件。。。。
    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;
    }