This is a migrated thread and some comments may be shown as answers.

[Solved] Add a RadGrid to GridTemplateColumn in GridTableView

1 Answer 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gijo
Top achievements
Rank 1
Gijo asked on 23 Nov 2009, 11:43 PM

Hi,

I am trying to add a RadGrid to GridTemplateColumn in GridTableView  . But it doesn't show up. Below is the code snippet. Am I missing something.

Thanks,
Gijo Joseph.





                            GridTableView tableView = new GridTableView(radGrid1);  
 
                            if (lastAddedDetailTable.ID == "RadGrid1")  
                            {  
                                radGrid1.MasterTableView.DetailTables.Add(tableView);  
                            }  
                            else  
                            {  
                                ((GridTableView)lastAddedDetailTable).DetailTables.Add(tableView);  
                            }  
 
                            //tableView.ItemsCreated += new GridItemEventHandler(radGrid1_ItemCreated);  
                              
                            //tableView.Width = Unit.Percentage(100);  
                            //tableView.Font.Bold = true;  
                            //tableView.BorderWidth = 2;  
                            //tableView.BorderColor = System.Drawing.Color.Red;  
 
                            if (filterElement.Attribute("parameters") != null)  
                                tableView.Name = filterElement.Attribute("parameters").Value;  
 
                            if (filterElement.Attribute("dataKeyNames") != null)  
                                tableView.DataKeyNames = filterElement.Attribute("dataKeyNames").Value.Split(',');  
 
 
 
                            string[] columnNames = filterElement.Attribute("columns").Value.ToString().Split(',');  
 
                            foreach (string columnName in columnNames)  
                            {  
                                GridBoundColumn boundColumn = new GridBoundColumn();  
                                tableView.Columns.Add(boundColumn);  
                                colName = columnName;  
                                if (colName.Contains("[F]"))  
                                {  
                                    boundColumn.Visible = false;  
                                    colNamecolName = colName.Substring(0, colName.IndexOf("[F]"));  
                                }  
                                boundColumn.UniqueName = colName;  
                                boundColumn.DataField = colName;  
                                boundColumn.HeaderText = colName;  
                                boundColumn.HeaderStyle.Font.Bold = true;  
                            }  
 
                            GridTemplateColumn templateColumn = new GridTemplateColumn();  
                            tableView.Columns.Add(templateColumn);  
                            templateColumn.ItemTemplate = new GridTemplate(filterElement.Attribute("name").Value); 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Web.UI;  
using Telerik.Web.UI;  
 
namespace Data.Reports  
{  
    public class GridTemplate : ITemplate  
    {  
         protected RadGrid childGrid;     
         private string colName;  
         public GridTemplate(string cName)  
         {  
             colName = cName;  
         }  
 
         public void InstantiateIn(System.Web.UI.Control container)  
         {  
             childGrid = new RadGrid();  
             childGrid.ID = colName;  
             //childGrid.NeedDataSource +=new GridNeedDataSourceEventHandler(childGrid_NeedDataSource);  
             childGrid.DataSource = DBClass.GetDataSP(Grid.referencePath, null);      
             container.Controls.Add(childGrid);  
 
         }  
 
         public void childGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
         {  
             childGrid.DataSource = DBClass.GetDataSP(Grid.referencePath, null);      
         }  
    }  

1 Answer, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 26 Nov 2009, 02:47 PM
Hello Gijo,

Thank you for your interest in our products.

To add a template column dynamically in a RadGrid, the grid should be created entirely in the code-behind in the Page_Init event handler. You should also add the column templates in there, so that the template controls can be added to the ViewState.

Please take a look at the Programmatic creation help topic for more information.

Regards,
Mira
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Gijo
Top achievements
Rank 1
Answers by
Mira
Telerik team
Share this question
or