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

Error on Postback while using Multi-Column Headers

13 Answers 415 Views
Grid
This is a migrated thread and some comments may be shown as answers.
kranthi kiran pusala
Top achievements
Rank 1
kranthi kiran pusala asked on 26 Apr 2013, 02:24 AM

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

13 Answers, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 30 Apr 2013, 08:18 AM
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.
0
Chip
Top achievements
Rank 2
answered on 14 Jun 2013, 07:06 PM
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.
0
Marin
Telerik team
answered on 19 Jun 2013, 07:48 AM
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.
0
Jorge Gracia
Top achievements
Rank 1
answered on 20 Jun 2013, 02:20 PM
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
0
Marin
Telerik team
answered on 20 Jun 2013, 03:41 PM
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.
0
Andrea
Top achievements
Rank 1
answered on 04 Sep 2013, 09:03 PM
Hi Marin,
in which release version can I find the fixes on viewstate of MultiColumn Headers?

Thank you
Regards
Andrea
0
Marin
Telerik team
answered on 05 Sep 2013, 07:20 AM
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.
0
Andrea
Top achievements
Rank 1
answered on 05 Sep 2013, 12:19 PM
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
0
Marin
Telerik team
answered on 05 Sep 2013, 12:35 PM
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.
0
Jorge Gracia
Top achievements
Rank 1
answered on 05 Sep 2013, 01:08 PM
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.

0
Resopi
Top achievements
Rank 1
answered on 25 Nov 2013, 09:02 PM
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...
0
DANIEL
Top achievements
Rank 1
answered on 23 Jan 2015, 11:17 PM
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";
0
Asim
Top achievements
Rank 1
answered on 15 Jul 2017, 12:41 AM

Thanks Daniel,

Your solution is working fine.

 

Tags
Grid
Asked by
kranthi kiran pusala
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Chip
Top achievements
Rank 2
Marin
Telerik team
Jorge Gracia
Top achievements
Rank 1
Andrea
Top achievements
Rank 1
Resopi
Top achievements
Rank 1
DANIEL
Top achievements
Rank 1
Asim
Top achievements
Rank 1
Share this question
or