int sum = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
int s1 = Int32.Parse(r.Cells[4].Value.ToString());
int s2 = Int32.Parse(r.Cells[5].Value.ToString());
sum = (sum + (s1 - s2));
r.Cells[7].Value = sum;
}
อันนี้โค้ดแสดงใน DG Code (C#)
string sql = "SELECT a.mem_id, a.fname, a.lname, d.lcont_id, b.lcont_amount_sal, s_sum=sum(d.sum_sal) FROM mem_h_member a LEFT JOIN loan_m_register b ON (a.mem_id = b.mem_id) and (a.br_no = b.br_no) LEFT JOIN loan_m_contact c ON (b.code = c.code) and (b.lreg_recno = c.lreg_recno) and (b.br_no = c.br_no) LEFT JOIN loan_m_paydept d ON (c.code = d.code) and (c.lcont_id = d.lcont_id) and (c.lreg_recno = d.lreg_recno) and (c.br_no = d.br_no)";
sql +="WHERE d.lcont_trancode <> 40";
sql += "AND c.l_type_code='" +StrType+ "'";
sql += "AND a.br_no='" + StrBranch + "'";
sql += "AND d.lpd_date BETWEEN '" + StrStartDate + "' AND '" + StrEndDate + "' GROUP BY a.mem_id, a.fname, a.lname, d.lcont_id,d.sum_sal, b.lcont_amount_sal order BY d.lcont_id,a.mem_id";
dt = new DataTable();
da = new OdbcDataAdapter(sql, conn);
da.Fill(dt);
conn.Close();
dataGridView1.DataSource = dt;
dataGridView1.Columns.Clear();
DataGridViewTextBoxColumn column;
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "mem_id";
column.HeaderText = "1";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "fname";
column.HeaderText = "2";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "lname";
column.HeaderText = "3";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "lcont_id";
column.HeaderText = "4";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "lcont_amount_sal";
column.HeaderText = "5";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "s_sum";
column.HeaderText = "6";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "s_tot";
column.HeaderText = "7";
column.Width = 100;
this.dataGridView1.Columns.Add(column);
dt = null;