Column grouping with autogeneratecolumns

7 posts, 0 answers
  1. Michael
    Michael avatar
    2 posts
    Member since:
    Oct 2015

    Posted 13 Nov 2017 Link to this post

    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?

  2. Michael
    Michael avatar
    2 posts
    Member since:
    Oct 2015

    Posted 15 Nov 2017 Link to this post

    Is my first post on this forum so difficult that nobody can figure it out?  :)
  3. Eyup
    Admin
    Eyup avatar
    3823 posts

    Posted 20 Nov 2017 Link to this post

    Hi Michael,

    I've already replied to this query in your formal support ticket. I suggest that we continue our technical conversation in only one of these threads.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  4. George
    George avatar
    17 posts
    Member since:
    May 2014

    Posted 23 Feb 2018 in reply to Eyup Link to this post

    Hi,

    I am also having the same error... May I know what is the solution?

    Thanks!

     

     

  5. Eyup
    Admin
    Eyup avatar
    3823 posts

    Posted 27 Feb 2018 Link to this post

    Hello George,

    I am attaching the provided sample to this thread, too. 
    I hope it will prove helpful in your scenario as well.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. George
    George avatar
    17 posts
    Member since:
    May 2014

    Posted 02 Mar 2018 in reply to Eyup Link to this post

    Hi Eyup,

    Thanks for your example, it works well if i only set for 1 ColumnGroupName,
    but i'm getting an error "Invalid column group configuration! Column "Description" cannot be after column "RowIndicator""
    if i want to set other columns to 2nd ColumnGroupName...


    protected void Page_Load(object sender, EventArgs e)
        {
            GridColumnGroup gcg1 = new GridColumnGroup();
            gcg1.HeaderText = "ColHeader";
            gcg1.Name = "ColHeader";
            dg.MasterTableView.ColumnGroups.Add(gcg1);

            GridColumnGroup gcg2 = new GridColumnGroup();
            gcg2.HeaderText = "ColData";
            gcg2.Name = "ColData";
            dg.MasterTableView.ColumnGroups.Add(gcg2);
    }

     

     

     

    protected void dg_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            
            if (e.Column.UniqueName.Contains("RowNo"))
            {
                e.Column.ColumnGroupName = "ColHeader";

            }
            else if (e.Column.UniqueName.Contains("ColName"))
            {
                e.Column.ColumnGroupName = "ColHeader";
                
            }
            else if (e.Column.UniqueName.Contains("ParentId"))
            {
                e.Column.ColumnGroupName = "ColHeader";
                
            }
            else if (e.Column.UniqueName.Contains("ChildId"))
            {
                e.Column.ColumnGroupName = "ColHeader";
                
            }
            else if (e.Column.UniqueName.Contains("Description"))
            {
    //error starts here if assign to second ColumnGroupName
                e.Column.ColumnGroupName = "ColData";

            }
            else if (e.Column.UniqueName.Contains("Qty"))
            {
                e.Column.ColumnGroupName = "ColData";
            }
            else if (e.Column.UniqueName.Contains("Items"))
            {
                e.Column.ColumnGroupName = "ColData";
            }
            else
            {
                e.Column.ColumnGroupName = "ColData";
            }
           
        }

     

     

     

     

     

  7. Eyup
    Admin
    Eyup avatar
    3823 posts

    Posted 07 Mar 2018 Link to this post

    Hello George,

    In this case, I suggest that you modify the provided sample to demonstrate this issue and open a formal support thread to send it back to us for further investigation. This will enable us to replicate the problem locally and provide more accurate and precise suggestions.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top