I want to make running total in datagridview. i tried some code on datagridview's sorted event but i didn't get the expected output.
My Code show's this type of data in DataGridView-
But I expect this output-

So Help me how can i solve it ?
- public void ShowData()
- {
- OleDbCommand cmd = new OleDbCommand("SELECT OB as OpenBalnce,PCB as CloasingBalance,LC as lcno from Balance where LC=" + txtticket.Text + "", con);
- OleDbDataAdapter da = new OleDbDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
-
- OleDbCommand cmd2 = new OleDbCommand("SELECT amount as Debit,LC as lcno,Narra as Narration,dt as Edate FROM voucher where DrCr='Dr' and FYear='" + cmbyear.Text + "' and LC=" + txtticket.Text + " order by dt ASC", con);
- OleDbDataAdapter da2 = new OleDbDataAdapter(cmd2);
- DataTable dt2 = new DataTable();
- da2.Fill(dt2);
-
-
- dt2.Merge(dt);
- dt2.AcceptChanges();
-
-
- OleDbCommand cmd1 = new OleDbCommand("SELECT LC as lcno,amount as Credit,Narra as Narration,dt as Edate FROM voucher where DrCr='Cr' and FYear='" + cmbyear.Text + "' and LC=" + txtticket.Text + " order by dt ASC", con);
- OleDbDataAdapter da1 = new OleDbDataAdapter(cmd1);
- DataTable dt1 = new DataTable();
- da1.Fill(dt1);
-
- dt1.Merge(dt2);
- dt1.AcceptChanges();
-
- dataGridView1.DataSource = dt1;
- dataGridView1.Sort(this.dataGridView1.Columns[3], ListSortDirection.Ascending);
- }
-
- private void dataGridView1_Sorted(object sender, EventArgs e)
- {
- for (int i = 1; i < dataGridView1.Rows.Count - 1; i++)
- {
- dataGridView1.Rows[i].Cells[5].Value = Convert.ToInt32(dataGridView1.Rows[i - 1].Cells[5].Value) + Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value) + Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
- dataGridView1.Rows[i-1].Cells[6].Value = Convert.ToInt32(dataGridView1.Rows[i-1].Cells[6].Value) + Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value) + Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
-
- }
-
-
- }