Search code examples
wpfwpf-controlswpfdatagridwpftoolkit

SUM of "Amount" column in DATAGRID's DataGridTemplateColumn RUNTIME


We have WPF application, In which we use DataGrid on one form. At runtime, when we Enter value in DataTemplate column, I need to Show SUM of that specific column in DATAGRID Footer. So when each time I change value in any Cell of That AMOUNT column, The correct SUM of that column need to be display. Which event I should try. I have tried this code , But it need to press tab each time, it does not display Correct SUM.

 private void dgInfo_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
 {
      Microsoft.Windows.Controls.DataGridRow row = this.dgInfo.ItemContainerGenerator.ContainerFromIndex(e.Row.GetIndex()) as Microsoft.Windows.Controls.DataGridRow;
      ContentPresenter CP = dgInfo.Columns[3].GetCellContent(row) as ContentPresenter;

      TextBlock t = FindVisualChild<TextBlock>(CP);
      if (t != null && t.Text.Length > 0)
      {
         decimal d = Convert.ToDecimal(t.Text);
         sum = sum + d;
         txtTotal.Text = sum.ToString();
      }
 }

Solution

  •  void dgInfo_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
            {
                           decimal tot = 0;
                           GetFaltyExpenseGridResult newRecord;
                           for (int i = 0; i < (dgInfo.Items.Count - 1); i++)
                           {
                               newRecord = (GetFaltyExpenseGridResult)((ContentPresenter)dgInfo.Columns[0].GetCellContent(dgInfo.Items[i])).Content;
    
                               if (newRecord != null)
                               {
    
                                       decimal d = Convert.ToDecimal(newRecord.Amount);
                                       tot = tot + d;
                                       txtTotal.Text = tot.ToString();
    
                               }
                           }
           }