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

dynamically add columns to existing radgrid

11 Answers 2328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tpowell
Top achievements
Rank 1
tpowell asked on 15 Sep 2008, 10:59 PM
I am having issues when I already have a grid built in the aspx code and am adding columns dynamically from the server side end. The grid will show up with the proper header titles for each column, but as soon as I sort the headers disappear and when I go to the next page each cell is filled with 'System.Data.DataRowView'.

Is their anyway I can make the columns persist in the viewstate? Thanks in advanced.

aspx code:
<telerik:RadGrid ID="Grid1" runat="server" AllowFilteringByColumn="True" 
        AllowPaging="True" OnNeedDataSource="Grid1_NeedDataSource" AllowSorting="True" 
        GridLines="None" ShowGroupPanel="True" AutoGenerateColumns="true" PageSize="50" 
        Skin="Telerik" Visible="false" Width="98%"
        <MasterTableView AutoGenerateColumns="False" > 
           <Columns> 
                
            </Columns> 
        </MasterTableView> 
 
        <ClientSettings AllowColumnsReorder="True" AllowDragToGroup="True" ReorderColumnsOnClient="True"
        </ClientSettings> 
    </telerik:RadGrid> 

c# code:
NewColumn = new GridBoundColumn();
NewColumn.DataField = 
"COLUMN1";
NewColumn.UniqueName = 
"COLUMN1";
NewColumn.Visible = 
false;
Grid1.MasterTableView.Columns.Add(NewColumn); 

11 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 1
answered on 16 Sep 2008, 05:27 AM
Hi,

Since you trying to add columns to a statically created Grid it is important to add column to the column collection first, before the values for their properties are set.

CS:
private void Page_Load(object sender, System.EventArgs e) 
 if ( !IsPostBack ) 
 { 
   GridBoundColumn boundColumn; 
 
   //Important: first Add column to the collection 
   boundColumn = new GridBoundColumn(); 
   this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
 
   //Then set properties 
   boundColumn.DataField = "CustomerID"
   boundColumn.HeaderText = "CustomerID"
 } 
       

Refer the following help article for more details.
Programmatic creation

Thanks
Shinu.
0
tpowell
Top achievements
Rank 1
answered on 17 Sep 2008, 07:17 PM
Thanks, works great now!
0
Bruno
Top achievements
Rank 1
answered on 18 Feb 2009, 06:34 PM
Hello,

and what if I want to programmatic add a new column to an existing Detail Table?

something like that.

<MasterTableView> 
   <detailtables> 
        <Gridtableview> 
 
            <columns> 
              <gridboundcolumn></gridboundcolumn
              <gridboundcolumn></gridboundcolumn
              <gridboundcolumn></gridboundcolumn
 
                How to insert more columns here, using the code behind? 
 
            <columns> 
        <gridtableview> 
 
   </detailtables> 
 
<columns>......</columns> 
<MastertableView> 

I used this successfully to add to the master table, but I cant add to the detail table.

                GridBoundColumn boundColumn; 
                boundColumn = new GridBoundColumn(); 
                RadGrid1.MasterTableView.Columns.Add(boundColumn); 
                boundColumn.DataField = "valor"
                boundColumn.HeaderText = "Valor"
                boundColumn.UniqueName = "valor"
                boundColumn.DataFormatString = "{0:R$###,###.##}"
How can I do the same with DETAILTABLES ?????

Thanxs


0
Chakravarthi
Top achievements
Rank 1
answered on 12 Jan 2010, 12:04 AM
Bruno:

I have similar requirement. did you find any solution for that? please let me know.

Thanks,
Chakri
0
Andy
Top achievements
Rank 1
answered on 12 Oct 2010, 01:06 PM
Same issue here with DetailTable. I add columns in DetailTableDataBind event handler. Tried it both ways - to add a column to the collection before setting it's properties and after, but they still disappear during the next post back.
Guys, please help!
0
tpowell
Top achievements
Rank 1
answered on 12 Oct 2010, 03:41 PM
I ran into this issue a few years ago, good thing I was the originator of this thread.

Help Documentation

"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." - Quote from the above article
0
Andy
Top achievements
Rank 1
answered on 13 Oct 2010, 07:47 AM
Thanx for reply! Well, in fact it does support mixed mode. I have a grid defined in aspx on Master page, with a few common columns (edit and delete). And on content pages I add the rest of the columns from code-behind. It works fine as long as I add columns to the collection before setting their properties.
As for detail table - I create them completely programmatically, so there're no issues too. I ran into problem when I needed to use "self-refferencing hierarchy" - the detail tables, which are created automatically, copy the structure of MasterTableView, but it seems to happen quite early in the life-cycle, thus, my custom columns (which I add from code-behind) are present in detail table only untill the first call-back occures.
We've found a solution though - I created a class which inherits RadGrid, and override OnInit,where I add custom columns to MasterTableView and set SelfHierarchySettings (KeyName and ParentKeyName). This allows custom columns and selfHierarchySettings to be added to the viewstate and they persist thru all call-backs.
0
Al
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 23 Nov 2015, 03:34 PM
[quote]Shinu said:Hi,

Since you trying to add columns to a statically created Grid it is important to add column to the column collection first, before the values for their properties are set.

CS:
private void Page_Load(object sender, System.EventArgs e) 
 if ( !IsPostBack ) 
 { 
   GridBoundColumn boundColumn; 
 
   //Important: first Add column to the collection 
   boundColumn = new GridBoundColumn(); 
   this.RadGrid1.MasterTableView.Columns.Add(boundColumn); 
 
   //Then set properties 
   boundColumn.DataField = "CustomerID"
   boundColumn.HeaderText = "CustomerID"
 } 
       


Refer the following help article for more details.
Programmatic creation

Thanks
Shinu. [/quote]

 

Is this still valid? you say the column must be added before it's properties are set but the article in the link says it must be done afterwards:

[quote]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:[/quote]

 

 

0
Eyup
Telerik team
answered on 25 Dec 2015, 12:29 PM
Hi Al,

There are 2 ways. First, if you check the different sections of the mentioned help article, you will see that the grid may be created declaratively in the aspx page and the columns added programmatically during Page_Load.

The second approach, which we recommend that you use, is creating the grid during Page_Init, which will enable you using Template columns and other dynamic changes. In this scenario, you first apply the settings and properties of the columns and finally add them to the MasterTableView.Columns collection:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/defining-structure/creating-a-radgrid-programmatically#creating-a-radgrid-on-page_init

I hope the clarification was helpful.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
William
Top achievements
Rank 1
answered on 07 Nov 2016, 08:30 PM
I looked at "Changing the Grid Structure Dynamically on Postback" and tried to implement this within UI for WPF, but none of methods or parameters are recognized, with the references and "using" statements correct. Is the RadGridView different than the RadGrid? Are the methods and parameters different between "UI for Asp.Net" and "UI for WPF"? Are the methods and parameters dependent on different versions of the software? 
0
Marin
Telerik team
answered on 09 Nov 2016, 03:34 PM
Hello,

Yes, the methods and parameters between the RadGrid from the UI for ASP.NET AJAX suite and the RadGridView from UI for WPF are completely different and cannot be used interchangeably. They normally do not change between different versions of the same suite. The RadGrid and RadGridView are very different controls and rely on different frameworks and do not share any common code.

Regards,
Marin
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Tags
Grid
Asked by
tpowell
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
tpowell
Top achievements
Rank 1
Bruno
Top achievements
Rank 1
Chakravarthi
Top achievements
Rank 1
Andy
Top achievements
Rank 1
Al
Top achievements
Rank 1
Iron
Iron
Veteran
Eyup
Telerik team
William
Top achievements
Rank 1
Marin
Telerik team
Share this question
or