2014-02-21 65 views
2

我將如何添加一行到這個datagridview的底部並加上所有上述行。行從數據表綁定。如何在datagridview中添加數據源與數據源的總和

DataGridView1.DataSource = DT; 
for (int i = 0; i < DataGridView1.Rows.Count; ++i) 
{ 
    sum += Convert.ToInt32(DataGridView1.Rows[i].Cells[col].Value); 
    DataGridView1.Rows.Insert(sum); 
} 

我試圖;脂肪酶在DataGridView1.Rows.Insert和。新增但我不確定我怎麼會是最好的做到這一點。

回答

1

嘗試添加頁腳

DataGridView1["Sum", dataGridView1.Rows.Count - 1].Value = sum; 

或嘗試這一個。

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
    { 
     double sum=0; 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      sum+= Convert.ToDouble(DataGridView1.Rows[i].Cells[col].Value); 
     } 
     var newrowindex= dataGridView1.Rows.Add(); 
     DataGridViewRow summaryRow = dataGridView1.Rows[newrowindex]; 
     summaryRow.Cells[col].Value =sum; 

    } 
0

你可以這樣做:

CS

private double _total; 
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    switch (e.Row.RowType) 
    { 
     case DataControlRowType.DataRow: 
      _total+=//increse the total 
     break 
     case DataControlRowType.Footer: 
      //Put the total in the right cell of the footer 
      e.Row.Cells[2].Text=_total.ToString(); 
     break; 
    } 
} 

標記

<asp:GridView ID="gv" ShowFooter="True" runat="server"> 

</asp:GridView>