2

Using Recursion I Want to Display the list of Contents to the datagridview in c# winforms.I tried by the below But as a result in datagridview Only Parent values displaying no Child Values Displayed

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

Conditions:-

    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;
    }

Form Load:-

    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";
    }

From the above code I got an output like this:-

     parent 0
     parent 1
     parent 2
     parent 3

I Need an Output Like this:-

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

Thank You..

ChrisF
  • 134,786
  • 31
  • 255
  • 325
JENKINS J
  • 153
  • 11
  • Are you using any 3rd party frameworks like devexpress? – Valentin Apr 08 '16 at 09:34
  • No Iam not using..Thanks.. – JENKINS J Apr 08 '16 at 09:39
  • You are probably want some sorft of TreeView, instead of dataGridView. Take a look here - http://stackoverflow.com/questions/9802724/how-to-create-a-multicolumn-treeview-like-this-in-c-sharp-winforms-app – Valentin Apr 08 '16 at 09:47
  • The winforms DataGridView cant do what I (Think) you are asking it to do, take a look at these: http://stackoverflow.com/questions/13306782/how-to-use-treeview-with-data-grid-view-column-in-c-sharp-windows-with-editable, http://stackoverflow.com/questions/4912873/treeview-with-columns, http://johnatten.com/2012/05/09/extending-c-listview-with-collapsible-groups-part-i/ - Do these fulfil your needs? – tolanj Apr 08 '16 at 09:50
  • Ok Sir Thanks for You Wonderful time...Now I will try with treeview control.... – JENKINS J Apr 08 '16 at 10:07
  • If I used treeview control means whether I need to give edit and delete control for each and every node line DataGridView.Thank You. – JENKINS J Apr 11 '16 at 09:05

1 Answers1

1

Finally I Got Answer For the Above Question Using Recursion .If Anybody Needs make Use of it Thank You:-

    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; }

    }

Form Load

    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();
    }

Loading all datas from database

    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;
    }

Load Only The Parent Values:-

    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;
    }

And Here Comes The Recursion Method:-

    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;
    }

Thank You:---

JENKINS J
  • 153
  • 11