0

フッターにグリッドビューの概要が必要です...

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

しかし、うまくいきません。何か案は?

4

2 に答える 2

2

このように、totalPrice と totalItems を global 変数として宣言します。

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

   }
于 2013-03-21T06:30:20.857 に答える
0

あなたのメソッドtotalPriceはメソッドスコープで宣言されているので、すべての行でゼロから始まります。フッター行になると、再びゼロになります。少なくとも 2 つのオプションがあります。

  1. SQL ステートメントに列を含めtotal = sum(price)、フッターにバインドします。
  2. totalPriceメソッドからページのプライベート フィールドに移動します。この場合、コードは機能するはずです
于 2013-03-21T06:30:29.410 に答える