簡體   English   中英

從gridview單元格求和值並使用標簽顯示它

[英]Sum value from gridview cells and display it using label

string total;
int totalmarks = 0;

foreach (GridViewRow row in GridView1.Rows)
            {

                int rowcount = this.GridView1.Rows.Count;

                for (int i = 0; i < rowcount; i++)
                {
                    total = row.Cells[3].Text;
                    totalmarks = Convert.ToInt32(total);

                    totalmarks +=totalmarks ;
                }


            }

            totalmarksLabel.Text = Convert.ToString(totalmarks);

我使用該部分代碼從gridview中的每一行獲取cell [3]的值。 然后我嘗試總結每一行中的每個值並使用標簽顯示它。 但是,當我運行我的代碼時,顯示的值是510,它應該是170。

行[1],行[2],行[3]的值分別為60,30和80。

我發現了問題,我有一個額外的循環。 在糾正之后,顯示的值是160而不是170.神秘的地方是另一個'10'消失了? 凌晨3點在馬來西亞,我被困住了。

我不會使用呈現給html的格式化值。 而是使用基礎數據項和總值。

var data = getdata();
grid.datasource = data;
grid.databind();
total.text = data.select(d=>d.value).sum().tostring();

我已經糾正了下面的代碼。 它的工作原理

int totalMarks=0;

        foreach (GridViewRow row in GridView1.Rows)
        {

                total = Convert.ToInt32(row.Cells[3].Text);

               totalMarks = totalMarks + total;



        }



        totalMarks Label.Text = Convert.ToString(totalMarks );

找出最佳方法,在應用程序上設置斷點。 然后在調試器模式下運行。

total = row.Cells[3].Text;

應該

total = row.Cells[i].Text;

暫無
暫無

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

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