0

我们有 WPF 应用程序,其中我们在一个表单上使用 DataGrid。在运行时,当我们在 DataTemplate 列中输入值时,我需要在 DATAGRID 页脚中显示该特定列的 SUM。因此,当我每次更改该 AMOUNT 列的任何单元格中的值时,都需要显示该列的正确 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();
      }
 }
4

1 回答 1

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

                           }
                       }
       }
于 2013-02-13T08:59:13.640 回答