2011-04-08 53 views
3

我正在開發一個winform應用程序應用程序。我想在每列的最後一行顯示列的總和。此行必須始終可見。DataGridView中的總行數

目前我正在考慮在記錄的datagridview下方添加另一個datagridview,並在底部的datagridview中顯示總和。

如果有更好的方法來完成這項任務?

+0

如何補充說,在其中您會綁定到網格的數據源 – V4Vendetta 2011-04-08 06:39:16

+0

嘗試了這一點 [頁腳添加到一個DataGridView] [1] [1]:http://stackoverflow.com/questions/13056678/datagridview-column-footer-c-net-winforms – 2013-12-20 22:04:26

回答

3

沒有,需要增加另外的datagridview

解決方案1:請參考this solution

解決方案2:如果上面的鏈接不正是你想要的話,

你可以嘗試手動添加最後一個摘要,在其中可以顯示您需要的信息。例如,你可以嘗試做如下:從數據庫

  1. 讀取數據,並填寫System.Data.DataTable
  2. 添加一列到新創建的數據表 - 該列可能設置 爲真最後,總結,排
  3. 編程方式添加一個額外的行包含適當的彙總數據
  4. 執行數據綁定的DataGridView控制
  5. ,利用適當的情況下,大膽或其他圖形不同摘要 行(行有額外的列值 設置爲true)
1

你可以做同樣的方式爲你的建議,就像把一個DataGridView顯示的總和。如果有更多的列,你也可以用這個來處理水平滾動。

另一種方法是有這個link

的另一種方式,你可以添加行到您的數據源本身顯示的總和。

0

即使這個問題是相當古老的,我想提出一個擴展到Niraj Doshis的答案。他的答案適用於數據綁定的DataGridView。我最近遇到了問題,需要在用戶可編輯的DataGridView中計算摘要,其解決方案的細節不同。無論如何,這也是非常簡單的。我正在更高的抽象層次上編寫我的函數,它概述了工作流程,但是忽略了實現細節。

所有你必須初始化的DataGridView首先,看

private void InitializeDataGridView() 
{ 
    SetColumnTypes(); 
    AddEmptyRow(); 
    AddSummaryRow(); 
} 

我設置DataGridView.AllowUsersToAddRowsfalse新行將位於摘要行下方。因此我添加了一個空行,用戶可以用他的數據填充。摘要行設置爲ReadOnly,因爲我們不希望我們的用戶編輯它。每當CellEndEdit提高我用下面的方法

private void UpdateDataGridView() 
{ 
    RemoveSummaryRow(); 
    RemoveEmptyRows(); 
    UpdateRowNumbers(); 
    AddEmptyRow(); 
    AddSummaryRow(); 
} 

首先我刪除了總結,所有的空行更新的DataGridView(你必須要小心。如果你想刪除剛纔編輯的行一個例外將被拋出,我還沒有想出如何做到這一點,但我只是做出瞭解決方案,當我想出解決方案時我會修改它。)之後,我在每個項目中設置一個運行編號行。這不是必需的,而是我的實現細節。最後,我再次添加一個空行,用戶可以使用它來添加更多數據,然後計算並添加彙總行。

正如我之前所說,這還不是一個現成的解決方案,而是我的概念,但有一些怪癖和錯誤。

0

在下面的例子中,我試圖利用DataGridView的NewRow。 DataGridView通過BindingSource綁定到DataTable。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      this.dataGridView1.RowPrePaint += dataGridView1_RowPrePaint; 
      this.dataGridView1.CellFormatting += dataGridView1_CellFormatting; 
      this.dataGridView1.CellValueChanged += dataGridView1_CellValueChanged; 
     } 

     void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.RowIndex != this.dataGridView1.NewRowIndex && e.ColumnIndex == 2) 
      { 
       this.dataGridView1.InvalidateRow(this.dataGridView1.NewRowIndex); 
      } 
     } 

     void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
     { 
      if (e.RowIndex == this.dataGridView1.NewRowIndex) 
      { 
       e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold); 
       e.CellStyle.ForeColor = Color.Red; 
       switch (e.ColumnIndex) 
       { 
        case 0: 
         e.Value = "Total"; 
         break; 

        case 2: 
         var sum = 0.0d; 
         for (int i = 0; i < this.dataGridView1.NewRowIndex; i++) 
         { 
          var value = this.dataGridView1[2, i].Value; 
          if (value is double) 
          { 
           sum += ((double)value); 
          } 
         } 
         e.Value = Math.Round(sum, 2); 
         break; 
       } 
      } 
     } 

     void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 
     { 
      if (e.RowIndex == this.dataGridView1.NewRowIndex) 
      { 
       e.PaintHeader(DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); 
       e.PaintCells(e.ClipBounds, DataGridViewPaintParts.All); 
       e.Handled = true; 
      } 
     } 

    } 
} 

下面是截圖

enter image description here