dynamically add columns to existing radgrid

8 posts, 1 answers
  1. tpowell
    tpowell avatar
    6 posts
    Member since:
    Aug 2012

    Posted 15 Sep 2008 Link to this post

    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" > 
            <ClientSettings AllowColumnsReorder="True" AllowDragToGroup="True" ReorderColumnsOnClient="True"

    c# code:
    NewColumn = new GridBoundColumn();
    NewColumn.DataField = 
    NewColumn.UniqueName = 
    NewColumn.Visible = 
  2. Answer
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Sep 2008 Link to this post


    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.

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

    Refer the following help article for more details.
    Programmatic creation

  3. tpowell
    tpowell avatar
    6 posts
    Member since:
    Aug 2012

    Posted 17 Sep 2008 Link to this post

    Thanks, works great now!
  4. Bruno
    Bruno avatar
    26 posts
    Member since:
    Feb 2009

    Posted 18 Feb 2009 Link to this post


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

    something like that.

                    How to insert more columns here, using the code behind? 

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

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


  5. Chakravarthi
    Chakravarthi avatar
    1 posts
    Member since:
    Jan 2009

    Posted 11 Jan 2010 Link to this post


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

  6. Andy
    Andy avatar
    38 posts
    Member since:
    Jun 2009

    Posted 12 Oct 2010 Link to this post

    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!
  7. tpowell
    tpowell avatar
    6 posts
    Member since:
    Aug 2012

    Posted 12 Oct 2010 Link to this post

    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
  8. Andy
    Andy avatar
    38 posts
    Member since:
    Jun 2009

    Posted 13 Oct 2010 Link to this post

    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.
Back to Top