2013-03-21 49 views
0

我需要頁腳gridview的總結......總結在GridView

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
     decimal totalPrice = 0M; 
     int totalItems = 0; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      Label lblPrice = (Label)e.Row.FindControl("lblPrice"); 
      decimal price = Decimal.Parse(lblPrice.Text); 
      totalPrice += price; 
      totalItems += 1; 
     } 
     if (e.Row.RowType == DataControlRowType.Footer) 
     { 
      Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice"); 
      lblTotalPrice.Text = totalPrice.ToString(); 
     } 
} 

但它不工作。有任何想法嗎?

回答

2

聲明totalPrice和TOTALITEMS作爲globel變量這樣

decimal totalPrice = 0M; 
int totalItems = 0; 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
    ... 

    } 
+0

是啊,它的工作..謝謝... – 2013-03-21 06:31:50

0

在你的方法totalPrice在方法範圍內聲明的,所以它會從零開始的每一行。當涉及到頁腳行時,它將再次爲零。至少有兩個選項:

  1. 包括total = sum(price)列到你的SQL語句,並結合 到您的頁腳;
  2. totalPrice移動到您方法頁面的私人字段。在 這種情況下你的代碼應該可以工作