Gridview添加合計行問題,浪費了2小時,寫出來共用一下

來源:互聯網
上載者:User

首先設定 showfooter=true;
在首頁定義兩個變數:  public float Mysum1;    public float Mysum2;

然後寫 RowDatabind方法:
 protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            base.GridView_RowDataBound(e, 1, 18);
            base.GridView_RowDataBound(e, 9, 28);
             DataKey Keys = GridView1.DataKeys[e.Row.RowIndex];
             TableCell myTableCell;
             myTableCell = e.Row.Cells[5];
             HyperLink LB = (HyperLink)(myTableCell.Controls[0]);
             LB.Enabled = Keys["SZLX"].ToString().Trim() == "0";
            DataRowView  MyRows=(DataRowView)e.Row.DataItem;
            Mysum1 += MyRows["SR"] is DBNull ? 0 : float.Parse(MyRows["SR"].ToString());
            Mysum2 += MyRows["ZC"] is DBNull ? 0 : float.Parse(MyRows["ZC"].ToString());
/* 注意這裡MyRows["SR"]網上的例子用的是索引號MyRows[6],這是假定你所顯示的欄位順序和資料來源的順序是一至的,事實上我們都 是用select* from 這樣的語句,所以用序號會出現錯誤,所以這個地方應該用欄位用而不是索引號 ,出現資料類型轉換錯誤我覺得可能就是這個原因造成的*/

        }
       

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "合計:";
            e.Row.Cells[6].Text = string.Format("{0:f2}", Mysum1);
            e.Row.Cells[7].Text = string.Format("{0:f2}", Mysum2);
        }

    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.