Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
70
XamGrid.Column.ValueConverter - Running Total (row index)
posted

Hi, this should be a typical use case but I neither find a reference in the forum nor luck when implementing...

 

We need for example a simple running total (but could be Running Average, or even some function across +/-n rows around the current one)

Let's say I have a column 'Values' = [0,1,2,3,4,5,6,...].

We now add an unbound column with a ValueConverter that should produce the running total, i.e. RunTotal = [0,1,3,6,10,15,21,...]

To do this, I need a reference to the row index, which I am unable to get a handle on. Any assistance, please?

 

Thanks!

 

 

  • 3255
    posted

    Hello mrth,

    You may find the index of the record using the below line of code

    ((Infragistics.Windows.DataPresenter.Record)(values[0])).RecordManager.Sorted.IndexOf((DataRecord)values[0]);

    
    

    I have created a sample for your requirement and below is the code snippet for your reference and attached is the sample.

     

    <igDP:UnboundField Name="Details">       
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings>
                                        <igDP:FieldSettings.CellValuePresenterStyle>
                                            <Style TargetType="{x:Type igDP:CellValuePresenter}" >
                                                <Setter Property="Template">
                                                    <Setter.Value>
                                                        <ControlTemplate  TargetType="{x:Type igDP:CellValuePresenter}">
                                                            <TextBlock TextAlignment="Right">
                                                                <TextBlock.Text>
                                                                    <MultiBinding Converter="{StaticResource runTotal}">
                                                                        <Binding Path="Record" RelativeSource="{RelativeSource Mode=TemplatedParent}"/>
                                                                        <Binding Path="DataItem.CurrentDueAmount"/>
                                                                    </MultiBinding>
                                                                    </TextBlock.Text>
                                                            </TextBlock>
                                                        </ControlTemplate>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </igDP:FieldSettings.CellValuePresenterStyle>
                                    </igDP:FieldSettings>
                                </igDP:Field.Settings>
    
                            </igDP:UnboundField>
    
     class RunTotalValue: IMultiValueConverter
        {
            #region IMultiValueConverter Members
    
            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {               
    
                if(values != null && values[0] != null)
                {
                    if(values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
                        return "";
    
                    double currentValue = 0;
                    double previousValue = 0;
                    Double.TryParse(values[1].ToString(),out currentValue);
                    int v_index =((Infragistics.Windows.DataPresenter.Record)(values[0])).RecordManager.Sorted.IndexOf((DataRecord)values[0]);
                    if(v_index > 1)
                    {
                        int newIndex = v_index - 1;
                        DataRecord v_previousRecord = (((Infragistics.Windows.DataPresenter.Record)(values[0])).RecordManager.Sorted)[newIndex];
                        Double.TryParse(v_previousRecord.Cells["CurrentDueAmount"].Value.ToString(), out previousValue);
                    }
                   
                    double newValue = currentValue + previousValue;
                    return newValue.ToString();
                }
                else
                    return null;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
            #endregion
        }
    

    Please let me know if you have any questions on this matter.


    Please note, we are making efforts to ensure all posts are addressed by an Infragistics expert. We believe that the other community members could benefit from this thread as well.

    XamDataGrid_RunTotal.zip