Hey ok here's what I have done. I don't think it is good practice to be adding the values the way you were initially.
If you values were in a text file why not load the text file?
I added an openFileDialog to the page which opens behind you button click, from there select your file(please save as tab delimited) From there I add the contents of the text file to a datatable by creating the DateSetGet function.
After the grid is loaded I get the average and display it in a newly added txtAverage textbox.
Hope this helps, if not come back and il be glad to help.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As DataTable
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.Title = "Select a text file"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim _lstErrors As New List(Of String)()
dt = DataSetGet(OpenFileDialog1.FileName, "/t", _lstErrors)
If dt.Rows.Count = 0 Then
MessageBox.Show("Please use a tab delimited text file to load the details", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
DataGridView1.Visible = True
Dim iSprsTotal As Decimal = 0
Dim iSprsColumnIndex = 1
If (DataGridView1.Rows.Count) Then
For Each objDataRow As DataRow In DataGridView1.Rows
iSprsTotal &= Convert.ToDecimal(objDataRow(iSprsColumnIndex))
Next
End If
txtAverage.Text = iSprsTotal / DataGridView1.Rows.Count
End If
End If
End Sub
Public Shared Function DataSetGet(filename As String, separatorChar As String, errors As List(Of String)) As DataTable
errors = New List(Of String)()
Dim table = New DataTable("Details")
Using sr = New StreamReader(filename, Encoding.[Default])
Dim line As String
Dim i As Integer = 0
While sr.Peek() >= 0
Try
line = sr.ReadLine()
If String.IsNullOrEmpty(line) Then
Continue While
End If
Dim values As String() = line.Split(New Char() {ControlChars.Tab})
If values.Length <= 2 Then
Return table
Else
Dim row = table.NewRow()
For colNum = 0 To values.Length - 1
Dim value As String = values(colNum)
If i = 0 Then
table.Columns.Add(value, GetType([String]))
Else
row(table.Columns(colNum)) = value
End If
Next
If i <> 0 Then
table.Rows.Add(row)
End If
End If
Catch ex As Exception
errors.Add(ex.Message)
End Try
i += 1
End While
End Using
Return table
End Function