[ASP.NET]動態新增GridViewRow

動態新增GridViewRow

自上一篇超短篇之後,我似乎不小心愛上這種快速的分享方式,哈哈!也許是沒有靈感吧..

 

今天要分享如何動態的新增GridViewRow,馬上就奉上Code吧!!

 


 protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow GVR = new GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
        TableCell tc_0 = new TableCell();
        tc_0.ColumnSpan = 5;
        tc_0.HorizontalAlign = HorizontalAlign.Right;
        tc_0.Text = "總計";
        GVR.Cells.Add(tc_0);
        Int32 sum = 0;
        for (Int32 i = 0; i < GridView1.Rows.Count; i++)
        {
            Label price = (Label)GridView1.Rows[i].Cells[2].FindControl("Label3");            //單價 
            TextBox num = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox1"); //數量 
            int total;
            total = int.Parse(price.Text) * int.Parse(num.Text);
            sum += total;
        }
        TableCell tc_1 = new TableCell();
        tc_1.Text = sum.ToString();
        tc_1.ForeColor = System.Drawing.Color.Red;

        GVR.Cells.Add(tc_1);
        GridView1.Controls[0].Controls.AddAt(GridView1.Rows.Count + 1, GVR); 
    }

 

 

 

然後我不知道怎麼結尾了,請慢慢享用囉。

分享


站作網站設計工作室 SiteMak Studio
Shinyo Ho 
Founder / Developer
email:shinyo.her@gmail.com|web:www.sitemak.com.tw