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

[Solved] RadGrid Runtime Columns for hierarchical grid

5 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Saurabh Srivastava
Top achievements
Rank 1
Saurabh Srivastava asked on 18 Aug 2009, 08:54 PM
Hi,

I have a hierarchical grid defined where I am programattically binding the detail tables. I am also generating some dynamic columns at runtime depending upon the user's input and adding them to the master as well as the detail tables.

Everything seems fine barring the issue of column names of the dynamic columns getting disappeared as soon as I expand the grid. The values still remain intact and so does the dataformatstring.

I see some discussions around this issue in the following thread but the suggestions provided does not work for me.
http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-runtime-columns.aspx

Here's my code snippet

   <telerik:RadGrid ID="rgScenario" runat="server" Width="100%" Skin="Custom" EnableEmbeddedSkins="false"   
                    AutoGenerateColumns="false" AllowSorting="false" AllowPaging="false" AllowFilteringByColumn="false" OnDetailTableDataBind="rgScenario_DetailTableDataBind" OnNeedDataSource="rgScenario_NeedDataSource">  
                        <MasterTableView DataKeyNames="xxxx" runat="server" EnableColumnsViewState="true">  
                            <DetailTables> 
                                <telerik:GridTableView DataKeyNames="yyyy" runat="server" Name="Detail1"  Width="100%" EnableColumnsViewState="true" > 
                                    <DetailTables> 
                                        <telerik:GridTableView DataKeyNames="zzzz" runat="server" Name="Detail2" Width="100%" EnableColumnsViewState="true">  
                                            <Columns> 
                                                <telerik:GridBoundColumn HeaderText="Area" DataField="Area" UniqueName="Area"></telerik:GridBoundColumn> 
                                                <telerik:GridBoundColumn HeaderText="Days" DataField="Days" UniqueName="Days"></telerik:GridBoundColumn> 
                                            </Columns>   
                                        </telerik:GridTableView>   
                                    </DetailTables> 
                                      
                                    <Columns> 
                                        <telerik:GridBoundColumn HeaderText="Start Area" DataField="StartAreaName" UniqueName="StartAreaName"></telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn HeaderText="End Area" DataField="EndAreaName" UniqueName="EndAreaName"></telerik:GridBoundColumn> 
                                    </Columns>    
                                </telerik:GridTableView>   
                            </DetailTables>   
                                      
                            <Columns> 
                                <telerik:GridBoundColumn HeaderText="Vessel" DataField="VesselType" UniqueName="Vessel"></telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn HeaderText="Days" DataField="TotalVoyageDaysNum" UniqueName="TotalVoyageDaysNum"></telerik:GridBoundColumn> 
                            </Columns>   
                        </MasterTableView>   
       
                    </telerik:RadGrid>  


 
 
 
public partial class ComparisonReport : System.Web.UI.Page  
{  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
        try  
        {  
                if (!IsPostBack)  
                {  
                    //Populate data into an IList say ParentIList  
                    ShowHideRiskColumns(input);  
 
                }  
                rgScenario.DataSource = ParentIList;  
            }  
        }  
        catch(Exception ex)  
        {  
              
        }  
          
    }  
 
    private void ShowHideRiskColumns(string input)  
    {  
        GridBoundColumn parentDynamicColumn = new GridBoundColumn();  
        parentDynamicColumn.HeaderText = "Some text";  
        parentDynamicColumn.UniqueName = "Some text";  
        parentDynamicColumn.DataFormatString = "{0:#,###.##}";   
 
        GridBoundColumn childDynamicColumnnew GridBoundColumn();  
        childDynamicColumn.HeaderText = "Some text";  
        childDynamicColumn.UniqueName = "Some text";  
 
        GridBoundColumn subChildDynamicColumn = new GridBoundColumn();  
        subChildDynamicColumn.HeaderText = "Some text";  
        subChildDynamicColumn.UniqueName = "Some text";  
          
        
        rgScenario.MasterTableView.Columns.Add(parentDynamicColumn);  
        rgScenario.MasterTableView.DetailTables[0].Columns.Add(childDynamicColumn);  
        rgScenario.MasterTableView.DetailTables[0].DetailTables[0].Columns.Add(subChildDynamicColumn);  
 
       switch(input)  
       {  
           case 1:  
        //Set the datafield according to the input  
               break;  
 
           case 2:  
              //Set the datafield according to the input  
               break;  
 
       }  
        
    }  
 
 
   protected void rgScenario_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    {  
        if (!e.IsFromDetailTable)  
        {  
            rgScenario.DataSource = iList;  
        }  
 
    }  
 
    protected void rgScenario_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)  
    {  
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;  
        switch (e.DetailTableView.Name)  
        {  
            case "Detail1":  
                {  
                    e.DetailTableView.DataSource = detailList1;  
                    break;  
                }  
 
            case "Detail2":  
                {  
                    e.DetailTableView.DataSource = detailList2;  
                    break;  
                }  
        }  
 
    }  
}  
 

5 Answers, 1 is accepted

Sort by
0
Saurabh Srivastava
Top achievements
Rank 1
answered on 19 Aug 2009, 09:06 AM
Hi,

Any responses?? Sorry but this is urgent.

Regards,
Saurabh
0
Saurabh Srivastava
Top achievements
Rank 1
answered on 21 Aug 2009, 07:55 AM
Hi,

Could someone please respond to this? It is urgent. I have changed all my .net grid implementations to telerik and need this query to be resolved for my final release.

Regards,
Saurabh
0
Shinu
Top achievements
Rank 2
answered on 21 Aug 2009, 08:18 AM
Hi Saurabh,

From the above post i can see that you trying to add columns both dynamically and declaratively. Telerik has mentioned in the following help article that '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.'
Programmatic creation

Shinu
0
Saurabh Srivastava
Top achievements
Rank 1
answered on 21 Aug 2009, 08:52 AM
Hi Shinu,

Thanks for the response. When you say mixing column declarations is not supported by Telerik, what does it mean? The grid functions exactly how I want it to behave apart from the display properties (DataFormatString) and the headertext. Are we saying that only the column rendering has an issue.

My scenario is that I have 15 columns defined statically and only one dynamic column. Surely, there has to be alternate ways to do this.

Ok, I am thinking of an alternate approah as I do not want to declare even my static columns dynamically.

Is there a way to dynamically assign the datafield of a column at runtime?

0
Saurabh Srivastava
Top achievements
Rank 1
answered on 21 Aug 2009, 01:20 PM
Hi Shinu,

Update regarding this.

I have been able to figure out a strategy for this and it has solved my problem. What I did was created the column and all its properties statically and then assigned the datafield at runtime based on user's inputs.

Thanks,
Saurabh


Tags
Grid
Asked by
Saurabh Srivastava
Top achievements
Rank 1
Answers by
Saurabh Srivastava
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or