DATAGRID的DataGridTemplateColumn RUNTIME中“Amount”列的SUM

时间:2013-02-13 04:14:14

标签: wpf wpf-controls wpfdatagrid wpftoolkit

我们有WPF应用程序,我们在一个表单上使用DataGrid。 在运行时,当我们在DataTemplate列中输入值时,我需要在DATAGRID页脚中显示该特定列的SUM。 因此,每当我在该AMOUNT列的任何Cell中更改值时,都需要显示该列的正确SUM。 我应该尝试哪个活动。 我试过这段代码,但每次都需要按Tab键,它不会显示正确的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();
      }
 }

1 个答案:

答案 0 :(得分:0)

 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();

                           }
                       }
       }