Completed
Last Updated: 20 Oct 2015 09:04 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 20 Nov 2014 08:22
Category: GridView
Type: Bug Report
1
FIX. RadGridView - calculated cells are not refreshed when a custom ExpressionContext is used with a custom function
Use the following code snippet:

public Form1()
{
    InitializeComponent();

    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Item", typeof(string));
    dt.Columns.Add("Price", typeof(decimal));

    for (int i = 0; i < 10; i++)
    {
        dt.Rows.Add(i % 5, "Item" + i, i * 2.25m);
    }

    this.radGridView1.DataSource = dt;
    GridViewDecimalColumn customCalculatedCol = new GridViewDecimalColumn("Custom Calculated Column");
    customCalculatedCol.Name = "Custom Calculated Column";
    customCalculatedCol.Expression = "SumIf(Id)";
    radGridView1.Columns.Add(customCalculatedCol);

    GridViewDecimalColumn customCalculatedCola = new GridViewDecimalColumn("Custom Col_A");         
    radGridView1.Columns.Add(customCalculatedCola);

    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;

    Telerik.Data.Expressions.ExpressionContext.Context = new CustomExpressionContext(radGridView1);
}

public class CustomExpressionContext : Telerik.Data.Expressions.ExpressionContext
{
    private RadGridView grid;

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

    public double SumIf(int currentId)
    {
        double countIf = 0;
        decimal sumIf = 0;
        foreach (GridViewRowInfo r in this.grid.Rows)
        {
            if ((int)r.Cells["Id"].Value == currentId)
            {
                countIf++;
                sumIf += (decimal)r.Cells["Price"].Value;
            }

           
        }
        return (double)sumIf;
    }
}

Workaround: invalidate the affected rows manually

private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "Price" || e.Column.Name == "Id")
    {
        foreach (GridViewRowInfo r in this.radGridView1.Rows)
        {
            if ((int)r.Cells["Id"].Value == (int)e.Row.Cells["Id"].Value)
            {
                r.InvalidateRow();
            }
        }
    }
}
0 comments