Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
849
Nested Groups using code
posted

Hello,

I have been trying to set up nested groups in a grid using code, but I am having much trouble with it. I have been using some designer code I generated as inspiration, but I have failed to get the result I want.

So I have decided to try with something very simple: group two columns in one group. But I have failed again... Here is the code I wrote:

            DataTable dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Columns.Add("Column2");
            dt.Rows.Add(dt.NewRow());

            this.ultraGrid1.DataSource = dt;

            this.ultraGrid1.DisplayLayout.Bands[0].RowLayoutStyle = RowLayoutStyle.GroupLayout;
            UltraGridGroup g;
            UltraGridColumn c;

            //Group
            g = new UltraGridGroup();
            g.Key = "Columns";
            g.RowLayoutGroupInfo.OriginX = 0;
            g.RowLayoutGroupInfo.OriginY = 0;
            g.RowLayoutGroupInfo.SpanX = 2;
            g.RowLayoutGroupInfo.SpanY = 2;

            //Column1
            c = this.ultraGrid1.DisplayLayout.Bands[0].Columns["Column1"];
            c.RowLayoutColumnInfo.OriginX = 0;
            c.RowLayoutColumnInfo.OriginY = 0;
            c.RowLayoutColumnInfo.SpanX = 1;
            c.RowLayoutColumnInfo.SpanY = 1;
            c.RowLayoutColumnInfo.ParentGroupIndex = 0;
            c.RowLayoutColumnInfo.ParentGroupKey = "Columns";

            //Column2
            c = this.ultraGrid1.DisplayLayout.Bands[0].Columns["Column2"];
            c.RowLayoutColumnInfo.OriginX = 1;
            c.RowLayoutColumnInfo.OriginY = 0;
            c.RowLayoutColumnInfo.SpanX = 1;
            c.RowLayoutColumnInfo.SpanY = 1;
            c.RowLayoutColumnInfo.ParentGroupIndex = 0;
            c.RowLayoutColumnInfo.ParentGroupKey = "Columns";

            this.ultraGrid1.DisplayLayout.Bands[0].Groups.Add(g);

Can anyone tell me what is wrong with my code?

Thanks in advance.

Best regards,

 

Damien

 

Parents
  • 469350
    Verified Answer
    Offline posted

    Hi Damien,

    The designer-generated code is often different from code you would use at run-time. The designer code relies on operations that only occur in the designer, like ISupportInitialize calls.

    Anyway, there are two problems with what you have here.

    The first is that you are creating a new group, but that group is never added to the band. In fact, you can't create a new group instance and then add it to the band, you have to use the band.Groups.Add method to add groups.

    The second problem is the way you are setting the parent group of the column.. or rather... that you are not setting it. ParentGroupIndex and ParentGroupKey are only used during deserialization. These properties are hidden from Intellisense deliberately so that you will not use them. You found them by looking at the designer code, of course. But that won't work. You need to use the ParentGroup property, instead.

    Here's your code, modified so that it works. I used the InitializeLayout event of the grid, which is a good place to do this kind of thing.


            private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
            {
                e.Layout.Bands[0].RowLayoutStyle = RowLayoutStyle.GroupLayout;
                UltraGridGroup g;
                UltraGridColumn c;

                //Group
                //g = new UltraGridGroup();
                //g.Key = "Columns";
                g = e.Layout.Bands[0].Groups.Add("Columns");
                g.RowLayoutGroupInfo.OriginX = 0;
                g.RowLayoutGroupInfo.OriginY = 0;
                g.RowLayoutGroupInfo.SpanX = 2;
                g.RowLayoutGroupInfo.SpanY = 2;

                //Column1
                c = this.ultraGrid1.DisplayLayout.Bands[0].Columns["Column1"];
                c.RowLayoutColumnInfo.OriginX = 0;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                //c.RowLayoutColumnInfo.ParentGroupIndex = 0;
                //c.RowLayoutColumnInfo.ParentGroupKey = "Columns";
                c.RowLayoutColumnInfo.ParentGroup = g;

                //Column2
                c = this.ultraGrid1.DisplayLayout.Bands[0].Columns["Column2"];
                c.RowLayoutColumnInfo.OriginX = 1;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                //c.RowLayoutColumnInfo.ParentGroupIndex = 0;
                //c.RowLayoutColumnInfo.ParentGroupKey = "Columns";
                c.RowLayoutColumnInfo.ParentGroup = g;

                this.ultraGrid1.DisplayLayout.Bands[0].Groups.Add(g);
            }

Reply
  • 849
    posted in reply to Mike Saltzman

    Hi Mark,

    Thanks for your help, it works now . :-) That was pretty much what I used at first but as my scenario was a bit more "advanced" than that, I got confused with the designer-code and thought he should be "more right" than me...

    Now that I am sure of the proper way to do it, I have moved to my actual scenario. Here is what I want:

    and here is what I got:

    Obviously, there is something wrong in the spans somewhere, but I can't figure where.

    Here is my code again:

           private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
            {
                e.Layout.Bands[0].RowLayoutStyle = RowLayoutStyle.GroupLayout;

                UltraGridGroup g0, g1;
                UltraGridColumn c;

                //Group Categories
                g0 = e.Layout.Bands[0].Groups.Add("Categories");
                g0.RowLayoutGroupInfo.OriginX = 0;
                g0.RowLayoutGroupInfo.OriginY = 0;
                g0.RowLayoutGroupInfo.SpanX = 2;
                g0.RowLayoutGroupInfo.SpanY = 3;
                g0.RowLayoutGroupInfo.LabelSpan = 2;

                //Column CategoryName
                c = e.Layout.Bands[0].Columns["CategoryName"];
                c.RowLayoutColumnInfo.OriginX = 0;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g0;

                //Column CategoryDescription
                c = e.Layout.Bands[0].Columns["CategoryDescription"];
                c.RowLayoutColumnInfo.OriginX = 1;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g0;


                //Group Cases
                g0 = e.Layout.Bands[0].Groups.Add("Cases");
                g0.RowLayoutGroupInfo.OriginX = 2;
                g0.RowLayoutGroupInfo.OriginY = 0;
                g0.RowLayoutGroupInfo.SpanX = 4;
                g0.RowLayoutGroupInfo.SpanY = 3;
               
                //Group Case1
                g1 = e.Layout.Bands[0].Groups.Add("Case1");
                g1.RowLayoutGroupInfo.OriginX = 0;
                g1.RowLayoutGroupInfo.OriginY = 0;
                g1.RowLayoutGroupInfo.SpanX = 2;
                g1.RowLayoutGroupInfo.SpanY = 2;
                g1.RowLayoutGroupInfo.ParentGroup = g0;

                //Column Case1SubCase1
                c = e.Layout.Bands[0].Columns["Case1SubCase1"];
                c.RowLayoutColumnInfo.OriginX = 0;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g1;

                //Column Case1SubCase2
                c = e.Layout.Bands[0].Columns["Case1SubCase2"];
                c.RowLayoutColumnInfo.OriginX = 1;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g1;

                //Group Case2
                g1 = e.Layout.Bands[0].Groups.Add("Case2");
                g1.RowLayoutGroupInfo.OriginX = 2;
                g1.RowLayoutGroupInfo.OriginY = 0;
                g1.RowLayoutGroupInfo.SpanX = 2;
                g1.RowLayoutGroupInfo.SpanY = 2;
                g1.RowLayoutGroupInfo.ParentGroup = g0;
               
                //Column Case2SubCase1
                c = e.Layout.Bands[0].Columns["Case2SubCase1"];
                c.RowLayoutColumnInfo.OriginX = 0;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g1;

                //Column Case2SubCase2
                c = e.Layout.Bands[0].Columns["Case2SubCase2"];
                c.RowLayoutColumnInfo.OriginX = 1;
                c.RowLayoutColumnInfo.OriginY = 0;
                c.RowLayoutColumnInfo.SpanX = 1;
                c.RowLayoutColumnInfo.SpanY = 1;
                c.RowLayoutColumnInfo.ParentGroup = g1;
            }

    I am not sure as to what origins and spans refer to. Do origins refer to the parent group? Do spans refer to the direct child of the group? To the lowest-level ones?

    Best regards,

    Damien

Children