This help article describes two options for creating a RadGrid instance dynamically, and provides code examples.
When defining the structure of a hierarchical grid (by either method), you should follow these rules:
Create the GridTableView objects for every detail table. You do not need to create an object for the MasterTableView of the grid, as this is created automatically when the RadGrid itself is created.
Add each detail table to the DetailTables collection of its parent table.
Ensure that each table in the grid has its own data source.
If you are using declarative data sources, set the DataSourceID property of each table to the ID of a DataSource object. For each parent table, set the DataKeyNames property to an array containing the names of each field in the table's data source that is used to link to detail tables. For each detail table, add GridRelationFields objects to the ParentTableRelation collection, each of which specifies a link between a field in the detail table to a field in the DataKeyNames array of the parent table.
If you are not using declarative data sources, add event handlers for the NeedDataSource and DetailTableDataBind events. In the DetailTableDataBind event handler, you can determine which data source should be related to the current table view by checking whether the GetDataKeyValue method of the detail table's ParentItem returns a value.
Hierarchical structure is not supported with simple data binding (calling DataBind()).
Dynamically Defining the Structure of a Statically-declared Grid
To define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. Columns and detail tables should be added to the corresponding collection first, before the values for their properties are set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.
Be sure to check that IsPostBack is False. Otherwise, you will end up adding the same objects to the RadGrid multiple times.
RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.
Creating columns in the Page_Load event handler does not work for template columns. For the controls inside a template to persist their ViewState, the grid must be generated completely in the code-behind using the Page_Init event (see below). That way, template controls are instantiated before the LoadViewState event of the page.
Creating the Grid Entirely in the Code Behind
When generating a RadGrid instance entirely in code, you should use the Page_Init or Page_Load event handlers.
Creating a RadGrid on Page_Load
When creating a RadGrid on a Page_Load event, the columns or detail tables should be added to
the corresponding collection first and then values for the properties of this instance should be
Creating a RadGrid on Page_Init
When generating a grid in the Page_Init event handler, grid columns should be added to the Columns collection of the MasterTableView after their attributes are set. No ViewState is required for grid structure to be persisted as it is recreated on each page initialization:
When creating a grid in the
event handler, you can use the Page_Load event to add an
AJAX setting for the grid to a RadAjaxManager so that the grid uses
Creating a Hierarchical Grid Programmatically
You should follow these basic steps in order to create hierarchical RadGrid
programmatically in the code-behind (having a data source control for data content generation):
Create the grid dynamically in the Page_Init
handler of the page by calling its constructor.
Specify the preferred settings for
your grid instance through its properties.
Create columns for the grid dynamically.
Keep in mind that you have to first set their properties and then add them to the
MasterTableView/GridTableView collection (discussed in the
of this same topic). Thus, their ViewState
will be properly persisted (as LoadViewState is raised after the
event of the page).
Set the proper
for the GridTableViews (along with their
and DetailKeyField attributes) and
for the MasterTableView/GridTableViews
in the code-behind of the page.
Assign data sources (through the
attribute) for each table in the grid hierarchy.If you do not want to use declarative relations,
generate the data in the NeedDataSource/DetailTableDataBind
handlers of the grid. On DetailTableDataBind you can determine
which data source should be related to the currently bound GridTableView by
checking its Name/DataSourceID property. Here, the Name
property must have a unique value for each detail table (this value has to be defined previously by the
developer) and the DataSourceID is the ID of the DataSource
control responsible for the corresponding detail table content generation.
Here is a complete source code sample:
Creating Template Columns Programmatically
When creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init
event. Then, you must create the templates dynamically in the code-behind and assign them to the ItemTemplate
and EditItemTemplate properties of the column. To create a template dynamically,
you must define a custom class that implements the ITemplate interface. Then you can assign an
instance of this class to the ItemTemplate or EditTemplateTemplate
property of the GridTemplateColumn object.
Column templates must be added in the Page_Init event handler, so that the template controls can be added to the ViewState.
The following example shows a Page_Init event handler that creates a grid, which includes a template column that shows data from multiple fields:
The code sample above results in the following grid:
For more information on creating templates programmatically, see the MSDN article: Creating Web Server Control Templates Programmatically.