Xceed Grid for WinForms v4.3 Documentation
Templates

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Templates

A template is an instance of a Row or Group that is used by the grid, to generate the rows and groups that will populate the grid once it is created.

Using group templates

When creating a grid that contains groups, it is necessary to let the grid know what template you want to use for each level of group in the grid. This is done by adding a Group object to the grid's list of group templates via the GroupTemplates property. 

The order in which the groups are added to GroupTemplates property will determine the order in which the grid is grouped. For example, if we add group1 and then group2 to the GroupTemplates property, the grid will first be grouped by group1 and then by group2 (see Example 1).

Keep in mind that in order for groups to have a visual representation (aside from their margin), they must contain at least one row that is not a data row in their header or footer sections. For example, a GroupManager row can be added to the header rows of a group, as demonstrated above, to group the rows like in the following diagram. 

If the group templates are modified after the grid is created, the UpdateGrouping method must be called in order for the modifications to be reflected in the grid.

Using the data row template

The DataRowTemplate is used as a template to create the other data rows that will populate the grid. The GridControl class, by default, uses a DataRow object with it's default settings to create other data rows (see Example 2).

If DataRowTemplate changes while the DataSource property is set, the changes will not automatically be reflected in the grid. In order for the modifications to be applied, the data source must be reassigned to the DataSource property.

Using detail grid templates

The number of DetailGrid objects added to the main grid's DetailGridTemplates collection indicates how many detail grids each DataRow in the main grid will have (see Examples 3).

If you want to add one or more detail grids to the DataRows contained within another detail grid, the same process is used: Configure a DetailGrid object that will be used a template to create the detail grids in the detail grid and add it to the detail grid's collection of DetailGridTemplates (see Example 4).

In order for the modifications made to the detail grid templates to be applied to the grid (after the grid has been populated) the UpdateDetailGrids method must be called.

Examples

Example 1: Using group templates

VB.NET
Copy Code
Imports Xceed.Grid
Dim group1 As New Group()
group1.GroupBy = "ShipCountry"
group1.HeaderRows.Add( New GroupManagerRow() )
group1.BackColor = Color.LightGray
Dim group2 As New Group()
group2.GroupBy = "ShipCity"
group2.HeaderRows.Add( New GroupManagerRow() )
group2.BackColor = Color.LightBlue  
gridControl1.GroupTemplates.Add( group1 )
gridControl1.GroupTemplates.Add( group2 )
' If you change the grouping after the data source was set, or data rows were
' added, you must call UpdateGrouping to refresh the groups. Ignore this line if
' you are creating group templates before setting the data source, or from inside
' a BeginInit() / EndInit() block.
gridControl1.UpdateGrouping()
C#
Copy Code
using Xceed.Grid;
 
Group group1 = new Group();
 
group1.GroupBy = "ShipCountry";
group1.HeaderRows.Add( new GroupManagerRow() );
group1.BackColor = Color.LightGray;
 
Group group2 = new Group();
 
group2.GroupBy = "ShipCity";
group2.HeaderRows.Add( new GroupManagerRow() );
group2.BackColor = Color.LightBlue;
  
gridControl1.GroupTemplates.Add( group1 );
gridControl1.GroupTemplates.Add( group2 );
 
// If you change the grouping after the data source was set, or data rows were
// added, you must call UpdateGrouping to refresh the groups. Ignore this line if
// you are creating group templates before setting the data source, or from inside
// a BeginInit() / EndInit() block.
gridControl1.UpdateGrouping();

Example 2: Using the data row template

VB.NET
Copy Code
GridControl1.DataRowTemplate.BackColor = Color.LightPink
GridControl1.DataRowTemplate.Height = 10 'pixels
C#
Copy Code
gridControl1.DataRowTemplate.BackColor = Color.LightPink;
gridControl1.DataRowTemplate.Height = 10; //pixels

Example 3: Using detail grid templates

VB.NET
Copy Code
Dim detail As New DetailGrid()
 
detail.HeaderRows.Add( New ColumnManagerRow() )
 
detail.DataSource = Nothing
detail.DataMember = "OrdersOrder_x0020_Details"
 
grid.DetailGridTemplates.Add( detail )
C#
Copy Code
DetailGrid detail = new DetailGrid();
detail.HeaderRows.Add( new ColumnManagerRow() );
detail.DataSources = null;
detail.DataMember = "OrdersOrder_x0020_Details";
grid.DetailGridTemplates.Add( detail );

Example 4: Using child detail grid templates

VB.NET
Copy Code
Dim childDetail As New DetailGrid()
 
childDetail.HeaderRows.Add( New InsertionRow() )
childDetail.HeaderRows.Add( New ColumnManagerRow() )
 
childDetail.Columns.Add( New Column( "Reference", GetType( String ) ) )
childDetail.Columns.Add( New Column( "Date Referred", GetType( DateTime ) ) )
 
detail.DetailGridTemplates.Add( childDetail )
C#
Copy Code
DetailGrid childDetail = new DetailGrid();
 
childDetail.HeaderRows.Add( new InsertionRow() );
childDetail.HeaderRows.Add( new ColumnManagerRow() );
 
childDetail.Columns.Add( new Column( "Reference", typeof( string ) ) );
childDetail.Columns.Add( new Column( "Date Referred", typeof( DateTime ) ) );
 
detail.DetailGridTemplates.Add( childDetail );