【问题标题】:How can I get the sum total of a column in the gridview to a textbox outside the gridview?如何将gridview中列的总和获取到gridview外部的文本框中?
【发布时间】:2014-11-27 04:30:34
【问题描述】:

在asp.net C#中,如何将gridview中列的总和获取到gridview外部的文本框中?

注意:Gridview 数据并非来自用户手动输入的任何数据库。

【问题讨论】:

    标签: c# asp.net gridview datagridviewcolumn


    【解决方案1】:

    在单击某个按钮或创建新行或您想要的任何事件时调用此方法

        private void GrandTotal()
        {
         float GTotal = 0f;
         for (int i = 0; i < GridView1.Rows.Count; i++)
         {
            String total = (GridView1.Rows[i].FindControl("lblTotal") as Label).Text;
            GTotal += Convert.ToSingle(total);
         }
         txtTotal.text=GTotal.toString();
        }
    

    希望我能帮上忙:D

    【讨论】:

    • 当我在没有数据的情况下加载时,我得到错误Object reference not set to an instance of an object.为什么?
    • 这可能是您的 Gridview1.Rows[i] 调用。如果 Gridview 为空,则不返回任何对象。
    【解决方案2】:

    试试这个

    int sum = 0;
    
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        sum +=Convert.ToInt32( GridView1.Rows[i].Cells[/*the column index*/].Text);
    }
    

    【讨论】:

    • 这取决于你什么时候想要这个计算。
    • 不,实际上我们不知道你想做什么,但在需要总结的地方使用它
    【解决方案3】:

    这应该可以完成工作。

    int total = 0;
    for (int row = 0; row  < GridView1.Rows.Count; row ++)
    {
        for (int col = 0; col < GridView1.Columns.Count; col++) 
             total +=Convert.ToInt32( GridView1.Rows[row].Cells[col].Text);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多