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

Dynamic Columns in a Grid and Grouping/Aggregate Problem

5 Answers 760 Views
Grid
This is a migrated thread and some comments may be shown as answers.
KJRB
Top achievements
Rank 1
KJRB asked on 19 Jun 2009, 05:22 AM
Hi, I have a grid that binds to a data table. Data table can have different number of columns. A user can select the view (grouping or not and if to show a data table A or B).
Everything works ok when there is no grouping.

When I add grouping: if a grid is bound to a data table with less columns (than the currently bound one) all works ok, but if a new data table has more columns I get an error that "Column 'Col 4' does not belong to table .

I am assuming that I do something wrong but so far could not see what it is. Could somebody point me in the right direction? The issue is: a grid is bound dynamically to differently structured data tables and when there is grouping added ... swicthing  to a table with more columns causes and error (Error only occurs when there is grouping and aggregate function in a footer).

I can supply a simple project that demonstartates this if this is not somethign obvious.The grid is RADGrid for ASP.NET AJAX and the version is 2009 Q1

Full Error:
-----------------------------------------------------------------------------------------------------------------------------
ArgumentException: Column 'Col 4' does not belong to table .]
   System.Data.DataRow.GetDataColumn(String columnName) +1775301
   System.Data.DataRow.get_Item(String columnName) +13
   Telerik.Web.UI.GridDataSetHelper.CalculateAggregates(DataRow SourceRow, DataRow DestRow, Int32 rowCountInGroup, GridTableView view) +704
   Telerik.Web.UI.GridDataSetHelper.InsertGroupByInto(GridTableView gridTableView, DataTable DestTable, DataTable SourceTable, String RowFilter, Int32 FirstIndexInPage, Int32 LastIndexInPage, Boolean ApplyPaging, Boolean IsCustomPaging) +669
   Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation() +2743
   Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable() +21
   Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray) +105
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +169
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +499
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   Telerik.Web.UI.GridTableView.PerformSelect() +4
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   Telerik.Web.UI.GridTableView.DataBind() +238
   Telerik.Web.UI.RadGrid.DataBind() +80
   Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason) +2122
   Telerik.Web.UI.RadGrid.Rebind() +9
   TelerikGridApp._Default.ddl_indexChanged(Object sender, EventArgs e) in C:\Projects\NRS\Utils\TelerikGridApp\Default.aspx.cs:68
   System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +111
   System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +134
   System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10
   System.Web.UI.Page.RaiseChangedEvents() +165
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1485

Thanks,

KJRB

5 Answers, 1 is accepted

Sort by
0
KJRB
Top achievements
Rank 1
answered on 23 Jun 2009, 01:26 PM
The workaround is to turn off autobinding and clearing and creating those dynamic columns from the code. 

What does not work is autobinding with grouping and aggregates without creating column first.
Actually:
Autobinding with grouping works as long as there are no aggregates.
Autobinding with grouping and aggregates works on postback though if columns were already added by the first Bind on the grid.
So in the end it si confusing if you just add Aggregate Function to a footer on  a column  and you start geting errors about column not found.
0
Tsvetoslav
Telerik team
answered on 24 Jun 2009, 01:54 PM
Hello KJRB,

Could you provide your aspx mark-up and code-behind (please, use the CodeFormatter tool of the editor to paste the code) or alternatively open up a formal support ticket and send a small test project to debug. It is important to know how you are creating the grid structure and binding the control.

Thank you.

Sincerely yours,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
KJRB
Top achievements
Rank 1
answered on 24 Jun 2009, 03:02 PM

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TelerikGridApp.Test" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager> 
    <div> 
    <asp:DropDownList runat=server ID=ddl OnSelectedIndexChanged="ddl_indexChanged" AutoPostBack="true">  
        <asp:ListItem Text="4 Items"  Value="4" /> 
        <asp:ListItem Text="7 Items"  Value="7" /> 
    </asp:DropDownList> 
      
        <asp:DropDownList runat=server ID=ddlGroup OnSelectedIndexChanged="ddl_indexChanged" AutoPostBack="true">  
        <asp:ListItem Text="Do not Group"  Value="Not" Selected="True" /> 
        <asp:ListItem Text="Group"  Value="Group"  /> 
    </asp:DropDownList> 
        <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" 
            onneeddatasource="RadGrid1_NeedDataSource" ShowFooter="true" 
            oncolumncreated="RadGrid1_ColumnCreated" > 
<MasterTableView ShowFooter="true" ShowGroupFooter="true" > 
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
</MasterTableView> 
        </telerik:RadGrid> 
    </div> 
    <telerik:RadAjaxManager runat="server">  
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="RadGrid1">  
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
      
    <asp:Label runat=server ID="lblInfo" /> 
    </form> 
</body> 
</html> 
 

Code Behind:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Data;  
using Telerik.Web.UI;  
 
namespace TelerikGridApp  
{  
    public partial class Test : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            this.lblInfo.Text = "";  
        }  
 
 
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            DataTable dt = GetSource(Int32.Parse(this.ddl.SelectedValue));  
 
            this.RadGrid1.DataSource = dt;  
            if (this.ddlGroup.SelectedIndex == 0)  
            {  
                this.RadGrid1.MasterTableView.GroupByExpressions.Clear();  
            }  
            else  
            {  
                if (this.RadGrid1.MasterTableView.GroupByExpressions.Count == 0)  
                {  
                    GridGroupByExpression gr = GridGroupByExpression.Parse("Yype [Type] Group By Yype");  
                    //CustomizeGroupByCommodityExpression(byCommodity);  
                    //this.RadGrid1.MasterTableView.ShowGroupFooter = true;  
                    this.RadGrid1.MasterTableView.GroupByExpressions.Add(gr);  
                }  
            }  
        }  
 
        private DataTable GetSource(int cols)  
        {  
            DataTable dt = new DataTable();  
 
            dt.Columns.Add("Description", typeof(string));  
            dt.Columns.Add("Yype", typeof(string));  
            for (int i = 0; i < cols; i++)  
            {  
                //DataRow r = new DataRow();  
                dt.Columns.Add("Col " + i, typeof(decimal));  
            }  
 
            for (int i = 0; i < 18; i++)  
            {  
                DataRow dr = dt.NewRow();  
                dr[0] = "Row " + i;  
                dr[1] = "type " + (i / 4);  
                for (int j = 0; j < cols; j++)  
                {  
                    dr[j+2] = 0.1m * j + 0.01m*j;  
                }  
 
                dt.Rows.Add(dr);  
            }  
 
            return dt;  
        }  
 
        protected void ddl_indexChanged(object sender, EventArgs e)  
        {  
            try  
            {  
                this.RadGrid1.Rebind();  
            }  
            catch (Exception ex)  
            {  
                this.lblInfo.Text += "<font color=red>Exception Type:" + ex.GetType().ToString() + "</font><br>";  
                this.lblInfo.Text += "<font color=red>Exception:" + ex.Message + "</font><br>";  
                this.lblInfo.Text += "<font color=red>Stack Trace:" + ex.StackTrace + "</font><br>";  
            }  
 
        }  
 
        protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)  
        {  
            if (e.Column is GridBoundColumn)  
            {  
                GridBoundColumn gbc = e.Column as GridBoundColumn;  
                gbc.FooterStyle.Wrap = false;  
                if (e.Column.UniqueName.StartsWith("Col "))  
                {  
                   gbc.Aggregate = GridAggregateFunction.Sum;  
                }  
            }  
   
        }  
 
    }  
}  
 

To see the problem select "Group" in the second dropdown and after that select "7" in the firts dropdown. You will see the error.
If you select "7" in the first dropdown and later "Group" it will work, if you switch later to 4 it will still work.

If you  change the markup  to make "Group" selected you will see the error on the first run. (I noticed that later on .. was following scenario #1 originally).

If you comment out the aggreagate line (gbc.Aggregate = GridAggregateFunction.Sum) there will be no error.  I got things working by adding setting up (adding to a grid) columns in Page_Load and setting

AutoGenerateColumns

 

="false" (in the previous post I meant to say AutoGenerateColumns and I said AutoBinding .. sorry for that).

I think it is me doing things wrong just not sure what is an explanation..

 

0
Accepted
Tsvetoslav
Telerik team
answered on 25 Jun 2009, 02:12 PM
Hello KJRB,

Having auto-generated columns in RadGrid and using aggregate functions is a specific scenario in which you need to set the UseAllDataFields property of the grid's MasterTableView to true. Please, try this suggestion and let us know of the result.

Thank you.

Best regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
KJRB
Top achievements
Rank 1
answered on 25 Jun 2009, 02:31 PM
Thanks! It was it. I experimented with: RetrieveAllDataFields (this one did not help) but never noticed UseAllDataFields.
Thanks a lot.
Tags
Grid
Asked by
KJRB
Top achievements
Rank 1
Answers by
KJRB
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or