Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
ok, so i have a text file, which i inputed into my data grid view and now i need to know how to write the code to find the average of the second column (the SprS column)?

this is what my dgv looks like:

 WP |   SprS|     E-M|      Cad|     Prog|
-------------------------------------------
57.9|   56.0|     62.1|    48.9|     12.4| 
67.8|   66.2|     75.6|    58.3|     18.8|
69.8|   68.0|     78.3|    61.8|     18.0| 
69.7|   66.9|     77.1|    59.0|     17.7|
68.1|   62.5|     76.5|    54.6|     15.1|
63.7|   53.8|     71.2|    46.1|     10.6|



the average is 62.23, but how do i write the code to get that answer????

thank you!
Posted
Updated 27-May-13 15:55pm
v5
Comments
_Damian S_ 19-May-13 23:22pm    
How could the average possibly be 373.4? The sum (total) might be that, but not the average.
M.L.S.14 23-May-13 18:48pm    
oops! obviously wasnt paying attention when i put the sum rather than the average down.
Ezra Neil 20-May-13 2:01am    
This should be pretty straightforward. What have you tried so far?
M.L.S.14 23-May-13 18:49pm    
nothing yet, i'm doing this assignment for my VB class and was absent on the day matrices and dgv's were explained so i'm unsure how to approach it & i cant quite grasp it on my own
Maciej Los 20-May-13 16:40pm    
What have you done till now?

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.

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       Dim dt As DataTable

       'Now set the file type
       OpenFileDialog1.FileName = ""
       OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
       'Set the starting directory and the title.
       OpenFileDialog1.InitialDirectory = "C:"
       OpenFileDialog1.Title = "Select a text file"

       'Present to the user.
       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

               'now if there are rows loop through to fine the average
               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

               'now display the average
               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 the is only one column here we can take that the file is in the wrong format.
                   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
 
Share this answer
 
Comments
M.L.S.14 31-May-13 15:01pm    
love your solution as it obviously gets me my answer, but it's beyond my expirience level as i am just starting out with VB. but thank you!!(:
frostcox 1-Jun-13 7:17am    
You can just loop through your datagrid like I do above and then display the average in a text box, I just showed you a real world example, need anymore help with anything let me know as I know starting out with programming can be tough!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900