RadMaskedNumericInput with a AggregateFunction only shows calculation after reload

1 Answer 31 Views
GridView
Matthias
Top achievements
Rank 1
Matthias asked on 20 May 2022, 08:31 AM

Hello,

I have this GridViewDataColumn "Real Quantity" which has a CellTemplate with a RadMaskedNumericInput,. If we put in a value (e.g. 0.02) and save it, it will fire an AggregateFunction which calculates the Qty remaining to 4.76. I tried to make this happen in real time while typing in a value without having to save and reload the page to show the calculation .

Does anyone have any idea how i could do this?

Regards,

Matthias

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 25 May 2022, 08:27 AM

Hi Matthias,

The updates of the aggregate results happens on commit edit of the cell. There is no API that allows to easily achieve your requirement, but here is an idea that you can try. Basically, you can subscribe to the ValueChanged event of RadMaskedNumericInput and re-add the aggregate functions. For example:

private void RadMaskedNumericInput_ValueChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
	var f = this.gridView.Columns[0].AggregateFunctions[0];
	this.gridView.Columns[0].AggregateFunctions.Clear();
	this.gridView.Columns[0].AggregateFunctions.Add(f);
}

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Matthias
Top achievements
Rank 1
commented on 25 May 2022, 01:31 PM

Hi Martin, 

Thanks for the answer. i tried the code but it didn't change anything for my problem, the value inside the RadMaskedNumericInput still does not change.

 

Regards,

Matthias

Martin Ivanov
Telerik team
commented on 27 May 2022, 09:17 AM

Can you send over some code showing your exact setup? A sample project will be even more useful.
Matthias
Top achievements
Rank 1
commented on 30 May 2022, 06:53 AM | edited

I am not allowed to share my project. I can send you a code snippet:

xaml:
<telerik:GridViewDataColumn x:Name="lblRealQuantity" Header="Real Quantity" Width="110" Focusable="True" Tag="Translate">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<Grid>
<telerik:RadMaskedNumericInput x:Name="rmniChangedQuantity" Value="{Binding RealQuantity, Mode=TwoWay}" FormatString="N2" SelectionOnFocus="SelectAll" BorderThickness="0" ValueChanged="RadMaskedNumericInput_ValueChanged"/>
</Grid>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
<telerik:GridViewDataColumn.AggregateFunctions>
<functions:RemainingQtyFunction ResultFormatString="{}{0:F}" />
</telerik:GridViewDataColumn.AggregateFunctions>
</telerik:GridViewDataColumn>

cs:
        private void RadMaskedNumericInput_ValueChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadMaskedNumericInput radVal = sender as RadMaskedNumericInput;

            DatabaseItem dbItem = radVal.DataContext as DatabaseItem;

            if (radVal.Value != null)
                dbItem.RealQuantity = Convert.ToDecimal(radVal.Value);

 

            else
                dbItem.RealQuantity = 0;

            foreach (var column in grid.Columns)

            {
if(column.AggregateFunctions[0] != null)
{
                var f = column.AggregateFunctions[0];
                column.AggregateFunctions.Clear();
                column.AggregateFunctions.Add(f);
}
            }
            }

        }

public class RemainingQtyFunction : AggregateFunction<DatabaseItem, string>
{
public RemainingQtyFunction()
{
this.AggregationExpression = items => RemainingQty(items);
}

private string RemainingQty(IEnumerable<DatabaseItem> source)
{
DatabaseItem firstItem = source.FirstOrDefault();

decimal totalQTY = firstItem.Quantity.Value * firstItem.OtherQuantity;
decimal remainingQTY = totalQTY - source.Sum(i => ((DatabaseItem)i).RealQuantity);

return string.Format("Qty remaining: {0} {1}", Math.Round(remainingQTY, 2), "kg");
}
}

Martin Ivanov
Telerik team
commented on 01 Jun 2022, 07:54 PM

Thank you for the code. I went through it but couldn't find where you are using the suggested code with removing and re-adding the aggregate function? Can you please add this at the end of the ValueChanged event handler and let me know how it goes?
Matthias
Top achievements
Rank 1
commented on 03 Jun 2022, 06:16 AM

I have edited the code snippit to include the removing and adding of the aggregate function.
Martin Ivanov
Telerik team
commented on 08 Jun 2022, 05:56 AM

I've tested the code and the aggregates are updated as expected. Can you try the attached project and let me know if I am missing anything?

Note that when you reset the aggregates the masked input control is losing the focus. I am afraid that there is proper way to avoid this.

Matthias
Top achievements
Rank 1
commented on 09 Jun 2022, 09:17 AM | edited

Thanks for the answer Martin. Indeed i cannot seem to find a proper way to avoid losing focus which is a bit of a problem for user experience with it. Does anyone know a good way to regain focus of a cell after the function?
Martin Ivanov
Telerik team
commented on 14 Jun 2022, 09:09 AM

There is no proper way to regain the focus or avoid the cell redrawing after resetting the aggregate functions. However, you can achieve the desired result by using the GroupHeaderTemplate property of RadGridView. This will allow you to implement custom UI and data bindings that are relevant to the values in the group and updated at runtime. 
Martin Ivanov
Telerik team
commented on 14 Jun 2022, 10:04 AM

I've attached a small example showing one way to use the GroupHeaderTemplate.
Tags
GridView
Asked by
Matthias
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or