簡體   English   中英

asp.net Gridview求和多列

[英]asp.net Gridview sum multiple column

我試圖動態求和列並添加為標簽。

我有一個名為“ GrdDynamic”的gridview

GrdDynamic_RowDataBound

protected void GrdDynamic_RowDataBound(object sender, GridViewRowEventArgs e)
{
int total = 0;

if (e.Row.RowType == DataControlRowType.Footer)
{
foreach (GridViewRow row in GrdDynamic.Rows)
{
   for (int i = 6; i <= GrdDynamic.Columns.Count; i++)
   {

       if (!string.IsNullOrEmpty(row.Cells[i].Text))
       {
        total = total + Convert.ToInt32(row.Cells[i].Text);
       }


      Label label = new Label();
      e.Row.Cells[i].Text = total.ToString();
      label.Text = "total" + " " + total;
      e.Row.Cells[i].Controls.Add(label);

   }
}
}
}

我需要根據6. column之后的每一列動態求和。

作為實例:

如果我有10列

我必須將6列加到10列,如下所示

6. column sum is 500

7. column sum is 350

8. column sum is 150

9. column sum is 100

10.column sum is 330

我該如何在代碼端做到這一點

任何幫助將不勝感激。

謝謝。

這行:

i <= GridView1.Columns.Count;

應該:

i < GridView1.Columns.Count;

否則,您將訪問不存在的列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM