Friday, March 21, 2014

Here Sample Code of Calculation in Datagrid Runtime



<DataGrid AutoGenerateColumns="False"
                  Grid.Row="2" 
                  HorizontalAlignment="Stretch"
                  Margin="0,2,0,0"
                  Name="CenterGrid"
                  IsSynchronizedWithCurrentItem="True"
                  VerticalAlignment="Top"
                  CanUserAddRows="True"
                  CanUserDeleteRows="True"
                
                  Focusable="True"
                
                  ItemsSource="{Binding Source={StaticResource DetailsViewSource}}"
                  TabIndex="4"  SelectionMode="Single"  >
           
            <!--KeyboardNavigation.TabNavigation="Cycle"-->
            <!--SelectionUnit="Cell"-->
            <DataGrid.Columns>
                <DataGridComboBoxColumn x:Name="cmbCentername" Header="Item Name"
                                            ItemsSource="{Binding Source={StaticResource ItemList}}"
                                            SelectedItemBinding="{Binding Path=ItemMaster}"
                                            DisplayMemberPath="ItemName"
                                            SelectedValuePath="ItemId"
                                             Width="150">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True" />
                                    <Setter Property="IsDropDownOpen" Value="True"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <DataGridTextColumn  x:Name="txtqty" Header="Qty" Width="*" Binding="{Binding Path=Qty,UpdateSourceTrigger=PropertyChanged}" />
                <DataGridTextColumn  x:Name="txtrate" Header="Rate" Width="*" Binding="{Binding Path=Rate,UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn  x:Name="txtamount" Header="Amount" Width="*" Binding="{Binding Path=Amount}" />
            </DataGrid.Columns>
        </DataGrid>
       
      
C# Code Behind
public partial class Inward :INotifyPropertyChanged
    {
        HTBookEntities book = new HTBookEntities();
        public object Gridinward()
        {
            var data =(from i in book.Inwards 
                      //select i;
                       join a in book.PartyMasters on i.PartyId equals a.PartyId
                       select new {i.InwardId,a.PartyName,i.InwardDate,i.BillNo,i.Remark,
                       i.AmountAdd,i.AmountLess,i.Paid,i.PaidRemark,i.Total }).ToList();
            return data;
        }

        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()
        {
            #region entitystate
            //if (this.EntityState == System.Data.EntityState.Unchanged
            //    || this.EntityState == System.Data.EntityState.Modified)
            //{
            //    // while loading back database changes, we want to preserve any modification\
            //    // that was made to our local instances
            //    this.InwardDetails.Load(System.Data.Objects.MergeOption.PreserveChanges);
            //}
             //calculate the new TotalAmount if there is any OrderItems found in the collection
            //var newAmount = InwardDetails == null ? 0 : InwardDetails.Sum(d => d.Amount);
            #endregion
            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));
            }
        }
    }

    public partial class InwardDetail : INotifyPropertyChanged
    {
        
        private void CalculateAmount()
        {
            Amount = Rate * Qty;
            NotifyPropertyChanged("Amount");
        }

        partial void OnRateChanged()
        {
            CalculateAmount();
        }

        partial void OnQtyChanged()
        {
            CalculateAmount();
        }

        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