2016-12-30 25 views
1

我想在我的DataGridView年底總結列的末尾總結列:如下所示如何在datagridview的

enter image description here

 column1 | column2 |column3| column3 | 
     --------+----------+-------+------------| 
     1  | 2  | 3 |  4  | 
    ---------+----------+-------+------------| 
     2  | 4  |  7 |  9  | 
--------------+----------+-------+------------| 
total 3  6   10   13 
----------------------------------------------- 
+0

你介意分享您的最新的代碼? –

+4

你有4個問題的歷史。沒有評論,沒有被接受的答案。顯示一些方面的問題的答案 – Badiparmagi

+0

我不是說最後的代碼 –

回答

0

可以在GridView控件的頁腳中添加總:

decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Amount")); 
        GridView1.FooterRow.Cells[1].Text = "Total"; 
        GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right; 
        GridView1.FooterRow.Cells[2].Text = total.ToString("N2"); 
+0

感謝>>>>> Rai Vu –

0

您可以使用DataBound事件,它的射擊畢竟行是databound,Amount總和我添加到標籤。標籤可在腳註中添加Total

protected void GridDataBound(object sender, EventArgs e) 
{ 
    GridView gridView = (GridView)sender; 
    DataTable dt= (DataTable)gridView .DataSource; 
    decimal sum= dt.AsEnumerable().Sum(r => r.Field<decimal>("Amount")); 
    totalLabel.Text = sum.ToString(); 
} 

或添加和作爲最後一行

protected void GridDataBound(object sender, EventArgs e) 
{ 
GridView gridView = (GridView)sender; 
DataTable dt = (DataTable)gridView.DataSource; 
decimal sum = dt.AsEnumerable().Sum(r => r.Field<decimal>("Amount")); 
var newRow = dt.NewRow(); 
newRow["Name"] = "Total"; 
newRow["Amount"] = sum.ToString(); 
dt.Rows.Add(newRow); 
} 
+0

感謝M.Wiśnicki –