Error on Postback while using Multi-Column Headers

14 posts, 0 answers
  1. kranthi kiran pusala
    kranthi kiran pusala avatar
    8 posts
    Member since:
    Jan 2010

    Posted 25 Apr 2013 Link to this post


    I am binding a radgrid to a dataset using NeedDataSource with AutoGenerateColumns set to true. I am using the following code to get Multi-Column Headers. On first load the code executes fine, but on post back it gives an error(pasted below).


    The error is happening in RadGrid_ColumnCreated. When I try to access column, it is null.

    In Page Load:
    if (!IsPostBack)
                   {
                       GridColumnGroup columnGroup = new GridColumnGroup();
                       columnGroup.HeaderText = "Parent Header";
                       columnGroup.Name = "ParentHeader";
                       RadGridControl.MasterTableView.ColumnGroups.Add(columnGroup);
                   }

      
    In RadGrid_ColumnCreated:  
    GridBoundColumn column = e.Column as GridBoundColumn;
          string columnName = column.HeaderText;    //--------------> Error happens here on postback.
     
       if (columnName.Contains("Name"))
                  {
                      GridBoundColumn boundColumn = e.Column as GridBoundColumn;
                      boundColumn.ColumnGroupName = "ParentHeader";
                   }

    Error:
    Invalid column name: 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Exception: Invalid column name: 

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace: 

    [Exception: Invalid column name: ]
       Telerik.Web.UI.GridTableView.CreateMultiColumnHeaders(Boolean useDataSource, GridColumn[] copiedColumnSet, GridTHead thead) +359
       Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +1183
       Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +640
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +182
       System.Web.UI.Control.EnsureChildControls() +146
       Telerik.Web.UI.GridTableViewBase.get_Controls() +20
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +34
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByID(ArrayList childState) +180
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +379
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +232
       System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +395
       System.Web.UI.Page.LoadAllState() +564
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1864
  2. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 30 Apr 2013 Link to this post

    Hi,

    The experienced behavior is expected. In order to resolve your issue you could move your GridColumnGroup declaration in the aspx RadGrid definition. Another possible approach is to create the entire grid in the Page_Load or Page_Init event as shown in the help article below. Note that it is not supported to create columns in the aspx definition and multi column headers in the code behind for the same grid.

    Regards,
    Antonio Stoilkov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Chip
    Chip avatar
    1 posts
    Member since:
    Jul 2011

    Posted 14 Jun 2013 Link to this post

    Why is that expected?

    I am having a similar issue, but I create my columns and column groups in the code behind dynamically and am still getting the "Invalid Column Name" on postback. Once the grid is set up, why should it matter if it is just Rebinding the datasource based on a filter? What is it about the postback that causes an invalid column?

    I'd like to use Multi Column Headers but cannot get past this issue so I'll have to leave them off.
  4. Marin
    Admin
    Marin avatar
    1057 posts

    Posted 19 Jun 2013 Link to this post

    Hello,

     The multicolumn headers are recreated on postback using data from the viewstate. If they are not setup correctly initially, their settings will be not be properly recreated on postback and an exception will be raised.
    We recommend following the below approach of dynamically creating the columns and column headers in the grid as shown in this help topic.
    A runnable example can also be found in this forum thread:
    http://www.telerik.com/community/forums/aspnet-ajax/grid/dynamically-generated-multicolumn-headers-top-header-disappears-after-postback.aspx

    Regards,
    Marin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  5. Jorge Gracia
    Jorge Gracia avatar
    6 posts
    Member since:
    Nov 2009

    Posted 20 Jun 2013 Link to this post

    I have the same problem, same error message.
    My code used to work just fine until I "accepted" to update my rad control to the latest version. The update broke my code, I wonder why it used to work and now it doesn't.

    Regards
  6. Marin
    Admin
    Marin avatar
    1057 posts

    Posted 20 Jun 2013 Link to this post

    Hi,

     The latest version of controls contains the most up to date improvements and fixes including for the MultiColumn headers. There might have been scenarios where the multicolumn headers are used in a incorrect way which can lead to further complications. That's why we improved their ViewState persistence and management to insure stability in all cases.
    The recommended valid approach of adding MultiColumn headers dynamically to the grid is shown in the links from my previous post.

    Regards,
    Marin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  7. Andrea
    Andrea avatar
    6 posts
    Member since:
    Jan 2009

    Posted 04 Sep 2013 Link to this post

    Hi Marin,
    in which release version can I find the fixes on viewstate of MultiColumn Headers?

    Thank you
    Regards
    Andrea
  8. Marin
    Admin
    Marin avatar
    1057 posts

    Posted 05 Sep 2013 Link to this post

    Hello,

     The latest version of the controls (2013.2.717) contains the most up to date fixes and improvements.
    Proper vewistate persistence of the multicolumn headers when they are dynamically created has been added since Q2 2012 SP1 (that is version 2012.2.724)

    Regards,
    Marin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  9. Andrea
    Andrea avatar
    6 posts
    Member since:
    Jan 2009

    Posted 05 Sep 2013 Link to this post

    Hi Marin,
    we are using the 2013.1.220.40 version but we are facing the problem of GridColumnGroup.

    We are creating the grid dinammically.
    On pageinit we
    • define the columns
    • set the datasource
    • bind the grid

    Then on the click of a button we have to change the source and rebind the grid.

    But we receive the error:

    Invalid column name:

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Exception: Invalid column name:

    Source Error
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [Exception: Invalid column name: ]
       Telerik.Web.UI.GridTableView.CreateMultiColumnHeaders(Boolean useDataSource, GridColumn[] copiedColumnSet, GridTHead thead) +537

    How can we solve?
    Thanks
    Andrea
  10. Marin
    Admin
    Marin avatar
    1057 posts

    Posted 05 Sep 2013 Link to this post

    Hello,

     The grid does not support changing of its structure and columns on button click precisely because in the ViewState the control has stored the old names of the columns. After the button click comes the rendering stage of the grid and it tries to render columns with names that do not exist any more - hence you get the error.
    That's why the only recommended event for dynamically adding of columns to the grid is Page_Init, which is fired before the ViewState is loaded.

    Regards,
    Marin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  11. Jorge Gracia
    Jorge Gracia avatar
    6 posts
    Member since:
    Nov 2009

    Posted 05 Sep 2013 Link to this post

    Andrea,

    I feel your pain, experienced that issue but yet haven't had opportunity to fix so opted to revert back to the last version it used to work for me.

    I wish would had now before upgrading that things were going to break, it would had me save time and rework. I now hesitate upgrading to the latest versions unless I know I have enough time for situations were my code will break due to Telerik changes.

  12. Resopi
    Resopi avatar
    4 posts
    Member since:
    Jun 2013

    Posted 25 Nov 2013 Link to this post

    Hi 

    If you have a static number os columns, you can use W.O.P Best Pratices, and do it: (lol i did hauhauhauah)

    in ASPx set ColumnsGroups
    <ColumnGroups>
        <telerik:GridColumnGroup Name="1" HeaderText="" HeaderStyle-HorizontalAlign="Center">
        </telerik:GridColumnGroup>
        <telerik:GridColumnGroup Name="2" HeaderText="" HeaderStyle-HorizontalAlign="Center">
        </telerik:GridColumnGroup>
        <telerik:GridColumnGroup Name="3" HeaderText="" HeaderStyle-HorizontalAlign="Center">
        </telerik:GridColumnGroup>
    </ColumnGroups>

    In PageLoad Use some Logic to set HeaderText
    int count = 0;
     
    foreach (var mes in Meses)
    {
        // Mine headertext is month and year
        GridFc.MasterTableView.ColumnGroups.FindGroupByName(count.ToString()).HeaderText = mes.Dt_Referencia.Month.ToString() + "/" + mes.Dt_Referencia.Year.ToString();
        count++;
    }


    And In ColumnCreated - set the ColumnGroup on your Column
    string Text =  fc.Dt_Referencia.Month.ToString() + "/" + fc.Dt_Referencia.Year.ToString();
     
    boundColumn.ColumnGroupName = GridFc.MasterTableView.ColumnGroups.FirstOrDefault(w => w.HeaderText==Text).Name;


    But Remember its a W.O.P. solution.... i made it becuse i dont have a time to reDo the Grid...
  13. DANIEL
    DANIEL avatar
    1 posts
    Member since:
    Jul 2012

    Posted 23 Jan 2015 Link to this post

    Hello everyone!

    I had the same problem creating the grid dynamically: Invalid column name:
    The problem was solved by adding GridColumnGroup in the collection before informing the name.

    before:

    var grp = new GridColumnGroup ();
    grp.Name = "group1";
    grid.MasterTableView.ColumnGroups.Add (grp);

    now:

    var grp = new GridColumnGroup ();
    grid.MasterTableView.ColumnGroups.Add (grp);
    grp.Name = "group1";
  14. Asim
    Asim avatar
    1 posts
    Member since:
    Dec 2012

    Posted 14 Jul 2017 in reply to DANIEL Link to this post

    Thanks Daniel,

    Your solution is working fine.

     

Back to Top