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.
Telerik.Web.UI.GridTableView.
Telerik.Web.UI.GridTableView.
System.Web.UI.WebControls.
System.Web.UI.Control.
Telerik.Web.UI.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Control.
System.Web.UI.Page.
System.Web.UI.Page.
13 Answers, 1 is accepted
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
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.
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
Marin
Telerik
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
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.
Marin
Telerik
in which release version can I find the fixes on viewstate of MultiColumn Headers?
Thank you
Regards
Andrea
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)
Marin
Telerik
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:
|
How can we solve?
Thanks
Andrea
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.
Marin
Telerik
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.
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...
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";
Thanks Daniel,
Your solution is working fine.