Home Forums WinForms controls Xceed Grid for WinForms Grouping columns programatically

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #16557 |

    Hi,

    I’m probably missing something obvious, but I having problems finding how to group a by column programatically. I can add a GroupByRow to the FixedHeaderRows so that the user can drag and drop columns to group them, however I want to achieve the same thing programatically and I can’t find the magic combination.

    Thanks in advance! 

    Cheers,
    Rob 

    Imported from legacy forums. Posted by Robert (had 792 views)

    User (Old forums)
    Member
    Post count: 23064

     Here is an example :

    Xceed.Grid.

    Group group1 = new Xceed.Grid.Group();

    Xceed.Grid.

    GroupManagerRow groupManagerRow1 = new Xceed.Grid.GroupManagerRow();

    group1.Collapsed =

    true;

    group1.GroupBy =

    “Country”;

    group1.HeaderRows.Add( groupManagerRow1 );

    gridControl1.GroupTemplates.Add( group1 );

     

    BTW, a good trick to learn how to do things programmatically is do to them in the designer, then look at the code that was generated, and then reproduce it in code.

    Imported from legacy forums. Posted by André (had 558 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi André,

    Thanks for the response. However the code does not seem to work in all cases. I need to use the grid in unbound mode and our forms are dynamically generated so we can’t use the designer. It’s quite hard to pick exactly what code generate by the designed is need and what is not. Here’s an example of code where I think it should be grouped on column item1, but is not:

      class Program {

        static void Main(string[] args) {

          // create the form

          Form form = new Form();

          // create and configure the grid

          GridControl grid = new GridControl();

          grid.Dock = DockStyle.Fill;

          grid.HeaderRows.Add(new GroupByRow());

          grid.HeaderRows.Add(new ColumnManagerRow());

          form.Controls.Add(grid);

          // add some columns

          grid.Columns.Add(new Column(“item1”));

          grid.Columns.Add(new Column(“item2”));

          grid.Columns.Add(new Column(“item3”));

          // add some rows

          var items = new string[] {“one”, “two”, “three”};

          foreach (var item in items) {

            DataRow row = grid.DataRows.AddNew();

            row.Cells[“item1”].Value = “const”;

            row.Cells[“item2”].Value = item;

            row.Cells[“item3”].Value = item;

          }

          grid.DataRows.AddNew(); // seems to be necessary to make last row appear

          // suggested code

          Xceed.Grid.Group group1 = new Xceed.Grid.Group();

          Xceed.Grid.GroupManagerRow groupManagerRow1 = new Xceed.Grid.GroupManagerRow();

          group1.Collapsed = true;

          group1.GroupBy = “item1”;

          group1.HeaderRows.Add(groupManagerRow1);

          grid.GroupTemplates.Add(group1);

          Application.Run(form);

        }

      } 

    Cheers,
    Rob

    BTW cool bunny picture! 

     

     

    Imported from legacy forums. Posted by Robert (had 602 views)

    User (Old forums)
    Member
    Post count: 23064

    Found the problem: you need to call UpdateGrouping if the grid already contains data.

    Cheers,
    Rob 

    Imported from legacy forums. Posted by Robert (had 918 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.