Friday, December 8, 2006

Implementing Xceed GroupRow and Enabling Row Grouping

Xceed Grid of 3.x Version has a very interesting feature Called Grouping. It allows you to show rows in groups and in a horizontal tree structure. Now let’s discuss about how to implement Xceed grid group feature. Well Xceed grouping feature works on columns that is you must group the rows on a particular column, but you can also use recursive grouping.

To add grouping feature first you need to add a Group Object. And we need to set the property “GroupBy”. “GroupBy” property of Group object takes a string as value which represents a column name of the grid. Bellow in code we have a simple example how to add a group by row.

private Xceed.Grid.Group group;

//in declaration section

//in initialization section

grid.Columns.Add( new Column( "DEPT", typeof( string ) ) );

grid.Columns["DEPT "].Title = "Department";

grid.Columns.Add( new Column( "NAME", typeof( string ) ) );

grid.Columns["NAME"].Title = "Employee Name";

group = new Group();

group.GroupBy = "DEPT";

grid.GroupTemplates.Add( group );


In the above example we have two column name and department. And we want to group employees by department. So groupby property of group object is set to the column name. And last of all you need to add the group object in the grid’s group template and call the update grouping method of the grid to activate the grouping.

Xceed by default adds a group header row. But we can always customize the group header and add our own customized group row. Bellow a simple example is given how to add group header row.

//in declaration section

private Xceed.Grid.GroupManagerRow gmrow;

//in initialization section

gmrow = new GroupManagerRow();

gmrow.Height = 24;

group.HeaderRows.Add( gmrow );

Xceed have GroupMangerRow for this purpose. You must add the GroupMangerRow to the header collection of the group object. And you can customize its Font, Fore Color, Title Format, Back Color, and other properties. Title format is very interesting. By default GroupMangerRow show the assigned groupby row column Title. But we can customize the Group by Header Row Title.

gmrow.TitleFormat = "%GROUPKEY% have %DATAROWCOUNT% Employees";

Here I have used only two keys to customize the Title format of the group header row. Please go to your Xceed Grid help and look for GroupManagerRow.TitleFormat property and look for more interesting feature to customized the group header row title.