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

Problem dynamically creating gridcalculatedcolumn

1 Answer 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tami
Top achievements
Rank 1
Tami asked on 25 Aug 2010, 09:01 PM

 

 

Hi,
I have a grid where I am dynamically creating all of my columns because I do not know the columns that will be returned at design time.  I also want to add a Total column at the end that is going to summarize the particular month range the user has selected.  My column is created correctly if I hardcode the months (see commented line below); however, if I try to build a similar string and pass that in the datafields, I get the error "Telerik.Web.UI.GridException: DataField "JAN10","FEB10","MAR10","APR10","MAY10","JUN10" for GridCalculatedColumn "Total" does not exist in current DataSource.".  You can see from the error message that my string is in the correct format.  I have seen posts on the forum regarding similar issues and it said that it was fixed in the latest release, but mine is still failing.  I am using 10.2. 

Thanks,
Tami

 

private void CreateCalcColumn(GridTableView oTableView, string sMonths) 
       
           GridCalculatedColumn totalColumn; 
           totalColumn = new GridCalculatedColumn(); 
           oTableView.Columns.Add(totalColumn); 
           totalColumn.UniqueName = "Total"
           totalColumn.Expression = "{0}+{1}"
           totalColumn.DataType = typeof(double); 
           totalColumn.DataFields = new string[] { lstMonths }; 
           //totalColumn.DataFields = new string[] { "JAN10", "FEB10" }; 
           totalColumn.Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum; 
           totalColumn.HeaderText = "Total"
           totalColumn.HeaderStyle.CssClass = "gridheader"
       }

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 31 Aug 2010, 08:44 AM
Hello Tami,

From your information I assume that lstMonths contains the comma separated names of months:

string lstMonths = "JAN10,FEB10";

In this case you can try the approach bellow:

private void CreateCalcColumn(GridTableView oTableView, string lstMonths)
{
    GridCalculatedColumn totalColumn;
    totalColumn = new GridCalculatedColumn();
    oTableView.Columns.Add(totalColumn);
    totalColumn.UniqueName = "Total";
    totalColumn.Expression = "{0}+{1}";
    totalColumn.DataType = typeof(double);
    totalColumn.DataFields = lstMonths.Split(',');
    totalColumn.Aggregate = Telerik.Web.UI.GridAggregateFunction.Sum;
    totalColumn.HeaderText = "Total";
     
}
 
I hope this helps.

Regards,
Martin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Tami
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or