Completed
Last Updated: 15 Aug 2017 10:20 by ADMIN
ADMIN
Dimitar
Created on: 21 Feb 2017 08:02
Category: GridView
Type: Bug Report
1
FIX. RadGridView - the SUM expression function does not work when the grid is grouped.
To reproduce:
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    DataTable dt = NewDt();
    this.radGridView1.DataSource = dt;      
    var c = this.radGridView1.Columns["Calculted"] as GridViewDecimalColumn;
    c.EnableExpressionEditor = true;
    c.Expression = "Column2 / SUM(Column2) * 10";
    c.DecimalPlaces = 2;
    c.FormatString = "{0:N2}";

    this.radGridView1.GroupDescriptors.Add("GroupByColumn", ListSortDirection.Ascending);
}

private DataTable NewDt()
{
    var dt = new DataTable();
    dt.Columns.Add("ItemColumn");
    dt.Columns.Add("GroupByColumn");
    dt.Columns.Add("Column2", typeof(decimal));
    dt.Columns.Add("Calculted", typeof(decimal));

    for (int i = 1; i <= 20; i++)
    {
        string gr = string.Empty;
        if (i % 3 == 0)
            gr = "gr3";
        else if (i % 2 == 0)
            gr = "gr2";
        else if (i % 5 == 0)
            gr = "gr5";
        else
            gr = "item" + i.ToString();

        dt.Rows.Add("id #" + i.ToString(), gr, i, 0);
    }
    return dt;
}

Workaround:
public class CustomExpressionContext : Telerik.Data.Expressions.ExpressionContext
{

    RadGridView radGridView;
    public CustomExpressionContext(RadGridView grid)
    {
        this.radGridView = grid;
    }

    public decimal MySum(string columnName)
    {
        decimal sum = 0;
        
        foreach (var item in radGridView.Rows)
        {
            sum += (decimal)item.Cells[columnName].Value;
        }
        return sum;
    }
}

 Telerik.Data.Expressions.ExpressionContext.Context = new CustomExpressionContext(this.radGridView1);
0 comments