2015-12-17 49 views
2

我使用下面的代碼來獲得DataField="Quantity"在GridView的總和,並顯示在GridView頁腳標題文本變爲零

的問題HeaderText="Quantity"變爲零

結果
protected void griddelverynote_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    int TotalQuantity = 0; 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    TotalQuantity += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity")); 
    else if (e.Row.RowType == DataControlRowType.Footer) 
    e.Row.Cells[1].Text = "Total Quantity"; 
    e.Row.Cells[2].Text = TotalQuantity.ToString(); 
} 

回答

0

你的代碼中的probelm是RowDataBound當它綁定時觸發每一行的事件。因此,每行TotalQuantity正在重新初始化爲0。聲明外部變量TotalQuantity: -

int TotalQuantity = 0; 
protected void griddelverynote_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
      TotalQuantity += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Quantity")); 
    else if (e.Row.RowType == DataControlRowType.Footer) 
    { 
      e.Row.Cells[1].Text = "Total Quantity"; 
      e.Row.Cells[2].Text = TotalQuantity.ToString(); 
    } 
} 
+0

謝謝,但我仍然有問題 – Ayman

+0

@Ayman - Got it!你錯過了else if塊中的花括號。檢查答案。 –

+0

同樣的問題HeaderText = 0 – Ayman