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

GridCalculatedColumn

6 Answers 446 Views
Grid
This is a migrated thread and some comments may be shown as answers.
3ms
Top achievements
Rank 2
3ms asked on 27 Oct 2008, 02:26 AM
I created a nested grid using the example programmatically from here.


I want to add a Calculated Column to a radgrid and added the following code but am unsure how to define the datafields and expression.

         calculatedColumn = new GridCalculatedColumn();
            calculatedColumn.Expression = "AmountBilled-AmountReceived";
            calculatedColumn.HeaderText = "Outstanding Balance";
            calculatedColumn.DataFormatString = "{0:C}";
            tableViewProjects.Columns.Add(calculatedColumn);

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Oct 2008, 05:22 AM
Hello,

Try setting the DataFields and the Expression attributes for the Calculated column as shown below:
cs:
 
  
 GridCalculatedColumn calcol = new GridCalculatedColumn();   
 calcol.DataFields =new string[]{"AmountBilled","AmountReceived"};   
 calcol.Expression ="{0}-{1}";   
 tableViewProjects.Columns.Add(calcol); 
 

Thanks
Princy.
0
3ms
Top achievements
Rank 2
answered on 27 Oct 2008, 03:02 PM
Princy,

Thanks for your help.  that got my columns working, now the formatting does not work.  Is there something I need to do other than what is here?

calculatedColumn = new GridCalculatedColumn();
            calculatedColumn.DataFields = new string[] {"AmountBilled", "AmountReceived"};
            calculatedColumn.Expression = "{0}-{1}";
            calculatedColumn.HeaderText = "Outstanding Balance";
            calculatedColumn.DataFormatString = "{0:c2}";
            tableViewProjects.Columns.Add(calculatedColumn);

Thanks,

3ms
0
Accepted
Princy
Top achievements
Rank 2
answered on 28 Oct 2008, 05:49 AM
Hello,

You have to set the DataType for the column so as to get the DataFormatString applied to the column. Check out the code below to find out how to get this done.
cs:
GridCalculatedColumn calcol = new GridCalculatedColumn();    
 calcol.DataFields =new string[]{"AmountBilled","AmountReceived"};    
 calcol.Expression ="{0}-{1}";   
 calcol.DataFormatString = "{0:c2}"
 calcol.DataType = System.Type.GetType("System.Double");  
 tableViewProjects.Columns.Add(calcol);  

Thanks
Princy.
0
Greg
Top achievements
Rank 1
answered on 14 Nov 2008, 03:28 PM
Is there any way to use something like a CalculatedColumn to calculate a value from code-behind...
 
Something like

C#:
CalculatedColumn value = getColValue(Column1 Value)

 

private string getColValue(string inputVal)   
{  
//Calcuations - returns a string  
}  
 



Shot.
Greg

 

0
Yavor
Telerik team
answered on 17 Nov 2008, 06:41 AM
Hello Greg,

You can use code-behind, to add the column dinamically, and set its properties and to specify which columns its value will be based on. Other than that, you can use code-behind to alter the value dynamically, by accessing the relevant cell.

All the best,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
3ms
Top achievements
Rank 2
answered on 09 Dec 2008, 09:29 PM
Princy,

Thanks for your help!  I added the following code to show the aggregate in the footer and I'm getting the error, "Sum is not supported for type System.Object".  This is all in the first detail table of my grid.  All is done on Page Load. 

RadGrid1.Width =

Unit.Percentage(98);

 

RadGrid1.PageSize = 10;

RadGrid1.AllowPaging =

true;

 

RadGrid1.AllowSorting =

true;

 

RadGrid1.PagerStyle.Mode =

GridPagerMode.NextPrevAndNumeric;

 

RadGrid1.AutoGenerateColumns =

false;

 

RadGrid1.ShowStatusBar =

true;

 

RadGrid1.ShowFooter =

true;

 

boundColumn =

new GridBoundColumn();

 

boundColumn.DataField =

"ContractAmount";

 

boundColumn.HeaderText =

"Contract Amount";

 

boundColumn.DataFormatString =

"{0:C}";

 

boundColumn.Aggregate =

GridAggregateFunction.Sum;

 

tableViewProjects.Columns.Add(boundColumn);

 

Tags
Grid
Asked by
3ms
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
3ms
Top achievements
Rank 2
Greg
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or