c1flexgrid

How to make parent child relationship in C1flexgrid


I am using C1Flexgrid and I need to make parent child relation in this grid. But child details need to show in same grid (no other grid ) and when I clicked on + expand should happen and vice versa.

I have written below code where I am having one column in datatable related to parent and child . If it is parent then I am making it 1 else 0.

When I tried with this code. R2 row is coming as child node of r which should not be a case as it is parent node.

Please help me on this .

 private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable("customers");
            dt.Columns.Add("abc");
            dt.Columns.Add("ddd");
            dt.Columns.Add("eee");
            dt.Columns.Add("parent");

            var r = dt.NewRow();
            r["abc"] = "11";
            r["ddd"] = "12";
            r["eee"] = "13";
            r["parent"] = "1";



            var r1 = dt.NewRow();

            r1["ddd"] = "12";
            r1["eee"] = "14";
            r1["parent"] = "0";

            var r2 = dt.NewRow();
            r2["abc"] = "11";
            r2["ddd"] = "1222";
            r2["eee"] = "14";
            r2["parent"] = "1";

            var rr32 = dt.NewRow();
            rr32["abc"] = "11";
            rr32["ddd"] = "1222";
            rr32["eee"] = "14";
            rr32["parent"] = "0";

            dt.Rows.Add(r);
            dt.Rows.Add(r1);
            dt.Rows.Add(r2);
            dt.Rows.Add(rr32);


            grid1.DataSource = dt;


            GroupBy("parent", 1);

            // show outline tree
            grid1.Tree.Column = 2;

            // autosize to accommodate tree
            grid1.AutoSizeCol(grid1.Tree.Column);
            grid1.Tree.Show(1);
        }

        void GroupBy(string columnName, int level)
        {
            object current = null;
            for (int r = grid1.Rows.Fixed; r < grid1.Rows.Count; r++)
            {
                if (!grid1.Rows[r].IsNode)
                {
                    var value = grid1[r, columnName];
                    string value2 = grid1[r, "parent"].ToString();

                    if (!object.Equals(value, current))
                    {
                        // value changed: insert node, apply style
                        if (value2.Equals("0"))
                        {
                            grid1.Rows.InsertNode(r, level);
                            grid1.Rows[r].Style = _nodeStyle[Math.Min(level, _nodeStyle.Length - 1)];
                            r++;

                        }






                        // show group name in first scrollable column
                        //grid1[r, grid1.Cols.Fixed+1] = value;

                        // update current value
                        current = value;
                    }
                }
            }
        }
    }

Solution

  • Your code was almost there, i have manipulated GroupBy method to fit your need. It solves your current requirement but you have to handle sorting and other functionalists of grid yourself.

    Hope this helps!

        void GroupBy(string columnName, int level)
        {
            object current = null;
            for (int r = grid1.Rows.Fixed; r < grid1.Rows.Count; r++)
            {
                if (!grid1.Rows[r].IsNode)
                {
                    var value = grid1[r, columnName];
                    if (!object.Equals(value, current))
                    {
                        // value changed: insert node, apply style
                        grid1.Rows.InsertNode(r, level);
                        grid1.Rows[r].Style = _nodeStyle[Math.Min(level, _nodeStyle.Length - 1)];
    
                        // show group name in first scrollable column
                        Row row = grid1.Rows[r + 1];
    
    
                        for (int i = 0; i < grid1.Cols.Count; i++)
                        {
                            grid1[r, i] = row[i];
                        }
    
                        grid1.Rows[r + 1].Visible = false;
                        r++;
                        // update current value
                        current = value;
                    }
                }
            }
        }