I have a requirement where I have to dynamically create the columns based on the number of people taking a test over a given period of time. There will be 2 column per employee (a "score" column and a "possible points" column). So what I would like to do is to put the person's name in the heading spanning these two columns. I have done this many times but never with auto generated columns. Here is a real stripped down version of what I am trying to do as well as the error I am receiving...
<
telerik:RadGrid
runat
=
"server"
ID
=
"grid"
AutoGenerateColumns
=
"true"
GroupingEnabled
=
"true"
OnDataBound
=
"grid_DataBound"
OnNeedDataSource
=
"grid_NeedDataSource"
/>
using System;
using System.Data;
using Telerik.Web.UI;
public partial class Mike : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void grid_DataBound(object sender, EventArgs e)
{
if (grid.MasterTableView != null && grid.MasterTableView.ColumnGroups.Count > 0)
{
foreach (GridColumn column in grid.MasterTableView.AutoGeneratedColumns)
{
if (column.UniqueName.Contains("ZoeBarnes"))
{
column.ColumnGroupName = "ZoeBarnes";
}
if (column.UniqueName.Contains("FrancisUnderwood"))
{
column.ColumnGroupName = "FrancisUnderwood";
}
if (column.UniqueName.Contains("RachelPosner"))
{
column.ColumnGroupName = "RachelPosner";
}
}
}
}
protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
GridColumnGroup gcg1 = new GridColumnGroup();
gcg1.HeaderText = "Zoe Barnes";
gcg1.Name = "ZoeBarnes";
grid.MasterTableView.ColumnGroups.Add(gcg1);
GridColumnGroup gcg2 = new GridColumnGroup();
gcg2.HeaderText = "Rachel Posner";
gcg2.Name = "RachelPosner";
grid.MasterTableView.ColumnGroups.Add(gcg2);
GridColumnGroup gcg3 = new GridColumnGroup();
gcg3.HeaderText = "Francis Underwood";
gcg3.Name = "FrancisUnderwood";
grid.MasterTableView.ColumnGroups.Add(gcg3);
DataTable table = new DataTable();
table.Columns.Add("ScoreZoeBarnes", typeof(int));
table.Columns.Add("PossibleZoeBarnes", typeof(int));
table.Columns.Add("ScoreRachelPosner", typeof(int));
table.Columns.Add("PossibleRachelPosner", typeof(int));
table.Columns.Add("ScoreFrancisUnderwood", typeof(int));
table.Columns.Add("PossibleFrancisUnderwood", typeof(int));
table.Rows.Add(10, 15, 14, 15, 12, 15);
grid.DataSource = table;
}
}
This should simply output one row of data with the person's name spanning the two columns relating to them. However I am getting the following error:
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
System.Web.UI.ControlCollection.get_Item(Int32 index) +9807837
System.Web.UI.WebControls.TableCellCollection.get_Item(Int32 index) +29
Telerik.Web.UI.GridHeaderItem.AdjustColSpan() +131
Telerik.Web.UI.RadGrid.ControlPreRender() +1084
Telerik.Web.UI.RadCompositeDataBoundControl.OnPreRender(EventArgs e) +44
System.Web.UI.Control.PreRenderRecursiveInternal() +88
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +985
Any idea why?