Hi guys

I have DataTable, and is bound to DataGridView. So, when the user inputs some values in 4 columns, it would calculate the sum and put it in the last 2nd last column.

Here's the code that I'm experimenting:
vb.net Code:
  1. Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
  2.  
  3.         Dim i As Integer
  4.         Dim intTotal As Integer = 0
  5.         For i = 5 To DataGridView1.Columns.Count - 3
  6.  
  7.             If Not DataGridView1.Item(e.RowIndex, i).FormattedValue = Nothing Then intTotal += Integer.Parse(DataGridView1.Item(e.RowIndex, i).FormattedValue.ToString)
  8.         Next
  9.         dt.Rows(e.RowIndex).Item("Total") = intTotal
  10.         'DataGridView1.Item(e.RowIndex, DataGridView1.Columns.Count - 2).FormattedValue = intTotal
  11.         'MessageBox.Show(e.RowIndex)
  12.     End Sub
But when the form is loaded, it would give an error saying out of bounds.

So, basically it should sum up certain columns when the user enters numbers into it and then display it in the last column.

I think, there would be a better way to do this. If you have some suggestions, let me know.

Thanks