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

Help with calculated column and query to RadGridView

1 Answer 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gabriel
Top achievements
Rank 1
Gabriel asked on 17 Feb 2014, 10:54 PM
Hello everyone,


I have a little problem with your calculated columns.
I need to get all calculated cells with value less than 0, but when I iterate to get the values < 0 I get fewer results. I guess is something related to the moment when is refreshed the calculated value. 

how can I get the count of cells with value < 0

right now I am making a linq query like this:

grid.Rows.OfType<GridViewDataRowInfo>().Count(x => Convert.ToDouble(x.Cells["columnName"].Value) <= 0);

but I get only 5 results. It seems that the refresh event of the calculated cells is fired when I scroll down, is that correct? can I force the program to calculate all of them?

thanks :)

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 20 Feb 2014, 02:14 PM
Hello Gabriel,

Thank you for contacting us.

The expression is being re-evaluated every time the Value property of a cell needs an update. You will need to force that update. The following recursive method can be used, to calculate the cells in all GridViewTemplates:
private int CountAllWithIdMoreThan0Recursively(GridViewTemplate template, Func<GridViewRowInfo, GridViewColumn, object> getValueFunc)
{
    int allCount = 0;
    foreach (GridViewTemplate templ in template.Templates)
    {
        allCount += this.CountAllWithIdMoreThan0Recursively(templ, getValueFunc);
    }
 
    foreach (GridViewRowInfo row in template.Rows)
    {
        allCount += Convert.ToInt32(getValueFunc(row, this.Grid.Columns[0]));
    }
 
    return allCount;
}

Before calling it, however there are some things that need to be executed to get that Func:
PropertyInfo accessorProperty = this.Grid.Columns[0].GetType().GetProperty("Accessor", BindingFlags.Instance | BindingFlags.NonPublic);
object accessorInstance = accessorProperty.GetValue(this.Grid.Columns[0]);
MethodInfo evaluateExpressionMethodInfo = accessorInstance.GetType().GetMethod("EvaluateExpression", BindingFlags.NonPublic | BindingFlags.Instance);
Func<GridViewRowInfo, GridViewColumn, object> getValueFunc =
    (Func<GridViewRowInfo, GridViewColumn, object>)Delegate.CreateDelegate(typeof(Func<GridViewRowInfo, GridViewColumn, object>),
    accessorInstance,
    evaluateExpressionMethodInfo);

Now you can call your method and get the correct results:
int count = this.CountAllWithIdMoreThan0Recursively(this.Grid.MasterGridViewTemplate, getValueFunc);

I hope this helps.

Regards,
George
Telerik
Tags
GridView
Asked by
Gabriel
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or