Shehroz Imran

Shehroz Imran

  • 2.1k
  • 22
  • 1.8k

C# datagridview sum two column and subtract with Value.

May 1 2020 3:33 AM
i have datatable with 2 columns,"Debit" & "Credit", and a variable which store my input opening balance. All i want is two get "account balance" by "OpeningBalance-(credit+Debit)".
 
Note: the debit and credit columns are dynamically as it depends on my date. So how to find opening balance with above mention formula apply on datagrid.
  1. //for column names and table names  
  2. dataGridView1.ColumnCount = 4;  
  3. dataGridView1.AutoGenerateColumns = false;  
  4. dataGridView1.Columns[0].HeaderText = "NewComments";  
  5. dataGridView1.Columns[0].DataPropertyName = "Comments";  
  6. dataGridView1.Columns[1].HeaderText = "NewDebit_Amount";  
  7. dataGridView1.Columns[1].DataPropertyName = "Debit_Amount";  
  8. dataGridView1.Columns[2].HeaderText = "NewCredit_Amount";  
  9. dataGridView1.Columns[2].DataPropertyName = "Credit_Amount";  
"dd=datatable name"
  1. for (int i = 0; i < dd.Rows.Count-1; i++)  
  2. {  
  3. // //dataGridView1.Rows[i].Cells[12].Value = (dataGridView1.Rows[i].Cells[5].Value.ToString()) +  
  4. // //(dataGridView1.Rows[4].Cells[2].Value.ToString()).ToString();  
  5. dataGridView1.Rows[i].Cells["NEWTotalPrice"].Value = (Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value.ToString())) + (Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value.ToString()));  
  6. }  
  7. dataGridView1.DataSource = dd;  
But it gives error, datagridview returns null.

Answers (4)