Friday, March 21, 2014

Here Sample code Get Sum of Amount in Final Textbox 

<GroupBox Grid.Row="3" Header="groupBox1" Height="87" HorizontalAlignment="Left" Margin="616,1,0,0" Name="groupBox1" VerticalAlignment="Top" Width="113">
                <Grid Width="102">
                    <TextBlock Height="15" HorizontalAlignment="Left" Margin="3,15,0,0" Name="textBlock4" Text="Add:" TextAlignment="Justify" VerticalAlignment="Top" />
                    <TextBlock Height="15" HorizontalAlignment="Left" Margin="1,44,0,0" Name="textBlock5" Text="Less:" TextAlignment="Justify" VerticalAlignment="Top" />
                    <telerik:RadWatermarkTextBox Height="23" Text="{Binding Path=AmountAdd,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="27,10,0,0" Name="txtAdd" VerticalAlignment="Top" Width="69" LostFocus="txtAdd_LostFocus" />
                    <telerik:RadWatermarkTextBox Height="23" Text="{Binding Path=AmountLess,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="27,39,0,0" Name="txtless" VerticalAlignment="Top" Width="69" LostFocus="txtless_LostFocus" />
                </Grid>
            </GroupBox>
            <telerik:RadWatermarkTextBox
            Height="23" Margin="563,93,0,0" Name="txtpaid"
            VerticalAlignment="Top" Grid.Row="3" Text="{Binding Path=Paid}" HorizontalAlignment="Left" Width="69" />
            <TextBlock Height="15" HorizontalAlignment="Left"
                   Margin="536,99,0,0" Name="textBlock7"
                   Text="Paid:" TextAlignment="Center"
                   VerticalAlignment="Top" Grid.Row="3" Width="29" />
            <TextBlock Height="15" HorizontalAlignment="Left"
                   Margin="648,98,0,0" Name="textBlock8"
                   Text="Total:" TextAlignment="Justify"
                   VerticalAlignment="Top" Width="32" Grid.Row="3" />
            <telerik:RadWatermarkTextBox Height="23" HorizontalAlignment="Right"
                                     Margin="0,93,12,0" Name="txttotal"
                                     VerticalAlignment="Top" Width="69"
                                     Grid.Row="3"
                                     Text="{Binding Path=Total,UpdateSourceTrigger=PropertyChanged}"/>

Code Behind
public partial class Inward :INotifyPropertyChanged
    {
        HTBookEntities book = new HTBookEntities();
      
        public Inward()
        {
            InwardDetails.AssociationChanged += new CollectionChangeEventHandler(InwardDetails_AssociationChanged);
        }
      
        private void InwardDetails_AssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            if (e.Action == CollectionChangeAction.Add)
            {
                var idetail = (InwardDetail)e.Element;
                idetail.PropertyChanged += (s, a) =>
                {  
                    if (a.PropertyName == "Amount")
                    
                    {
                        SumAmount();
                    }
                };
            }
            SumAmount();
        }

        public void SumAmount()
        {
          
            int Sum = InwardDetails.Sum(i => i.Amount);

            Total = Sum + AmountAdd - AmountLess;
            NotifyPropertyChanged("Total");
        }
        partial void OnAmountAddChanged()
        {
            SumAmount();
        }
        partial void OnAmountLessChanged()
        {
            SumAmount();
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {

                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

    }

0 comments :

Post a Comment

Powered by Blogger.

Followers

About

Popular Posts