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

RagGrid Aggregate when no DataField

1 Answer 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 10 Oct 2011, 04:11 PM
Hi,
Is there any way to get a sum aggregate on a column when there is no DataField?  We have a RadGrid where we need to fill some of the cells from ItemDataBound because our data item has in it a list of custom field values.  The ItemDataBound method is here:
protected void WorkOrderGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        WorkOrderDetailItem view = item.DataItem as WorkOrderDetailItem;
        List<CustomField> CustFlds = CustFields();
        for (int j = 0; j < CustFlds.Count; j++)
        {
            item.Cells[FirstCustomFieldCell + j].Text = (string.IsNullOrEmpty(view.CustomFieldValues[j]) ? " " : view.CustomFieldValues[j]);
        }
    }
}
Now, some of the custom fields are numeric and we need to do aggregates on them.  These fields are added to the grid this way:
protected void AddCustomFieldColumnsToGrid(List<CustomField> customFields)
{
    foreach (CustomField custFld in customFields)
    {
        if (custFld.CustomFieldType == "money")
        {
            GridNumericColumn col = new GridNumericColumn();
            _reportGrid.Columns.Add(col);
            col.DataType = typeof(Decimal);
            col.DataFormatString = "$ {0:N}";
            col.Aggregate = GridAggregateFunction.Sum;
            col.UniqueName = "cf" + custFld.Id.ToString();
            col.HeaderText = custFld.Name;
            col.Display = false;
        }
        else
The problem is that this code results in a "Exception has been thrown by the target of an invocation." error with an InnerException of "No matching constructor in type 'Decimal?'"  When I comment the Aggregate line of the column definition, I get the expected values but the DataFormatString is not applied.  This lead me to wonder if the issue is that I am setting the cell text in the ItemDataBound instead of giving a DataField, which I can't do.  Any suggestions you have would be greatly appreciated.

Thanks,
Dan Norton

1 Answer, 1 is accepted

Sort by
0
Accepted
Iana Tsolova
Telerik team
answered on 11 Oct 2011, 02:49 PM
Hello Dan,

No, you need to have DataField set for the column in order the built-in aggregates to work. Furthermore the aggregates are calculated based on the data available in the grid DataSource, not based on the data you lately display in the grid. So in your case if it best to manually perform the calculations and set them as Text for the particular column footer cell.

Kind regards,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or