2014-02-21 4 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); 
} 

I가 시도; DataGridView1.Rows.Insert 및 .Add에서 PPL하지만 내가이 일을 가장 좋은 것이 얼마나 확실입니다.

답변

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

이 작업을 수행 할 수 있습니다 :

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> 

CS

관련 문제