Results 1 to 10 of 10

Thread: [RESOLVED] getting an average in a datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Resolved [RESOLVED] getting an average in a datagridview

    Hi all
    Below I have code to sum up a list of numbers in a specific column in my datagridview,but is there a way I can take the first five numbers of the column and find the average figure,and then place that average figure into a text box on my form.

    Code:
     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim sum As Double = 0
            Dim i As Single
            For i = 0 To DataGridView1.RowCount - 1
                sum +=
                DataGridView1.Rows(i).Cells("examfigs").Value()
            Next
            TextBox1.Text = sum      
    
        End Sub
    kind regards
    al

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: getting an average in a datagridview

    I don't think that you have really thought this through. You have a loop that goes from 0 to the index of the last row in the grid. If you only want to use five rows then does it not make sense to change the upper limit of the lop to the index of the fifth row in the grid, i.e. 4?

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: getting an average in a datagridview

    Hi,
    Thanks for your reply.I see what you mean but what code would I use to actually average the numbers up.
    Kind regards
    al

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: getting an average in a datagridview

    Again, think it through. We all learned how to calculate an average in school. You simply add the values together and divide by the number of values. You're already doing the first part.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: getting an average in a datagridview

    Hi JM,
    got it I think
    This works but does it look OK to an expert like yourself.Many thanks for your suggestions encouragement.
    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim sum As Double = 0
            Dim i As Single
            For i = 1 to 5
                sum +=
                DataGridView1.Rows(i).Cells("examfigs").Value()
            Next
            TextBox1.Text = sum      
            TextBox1.Text = sum/5
        End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: getting an average in a datagridview

    If 'i' is being used to store Integer values then why is it declared as type Single?

    You were starting your loop counter at 0 before and you have now changed it to 1. Why is that? Do you want to start at the second row?

    There's no point setting the Text of the TextBox to 'sum' if what you actually want it set to is 'sum/5' and that's what you actually set it to immediately afterwards. If you want a wall painted blue, would you paint it red first and then immediately paint it blue over the top?

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: [RESOLVED] getting an average in a datagridview

    Hi Jm,
    Thanks for the reply.Starting the loop at zero only took the first 4 numbers in,1 to 5 took the five numbers in.Good point about setting the sum.I never thought of that.Do you mean like this?
    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim sum As Double = 0
            Dim i As Single
            For i = 1 to 5
                sum +=
                DataGridView1.Rows(i).Cells("examfigs").Value()
            Next
            TextBox1.Text = sum/5      
           
        End Sub
    What would you set the i int to, perhaps a double.?

    Kind Regards
    albert

  8. #8
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    Re: [RESOLVED] getting an average in a datagridview

    If you look at the DataGridView1.RowCount value you will note it is an integer. You would use an integer to iterate through collections/arrays like this and which all are zero index based i.e. the first index position starts at 0. In your case the first row will be 0.

    You can include the count variable in the construction of the for/loop like this,

    Code:
    For i as integer = 0 to 4 ' process the first 5 rows 
    sum +=  DataGridView1.Rows(i).Cells("examfigs").Value()  ' Do something with each row
    Next

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: [RESOLVED] getting an average in a datagridview

    Hi,

    if you use a Datatable, you can use this...

    I want to get the avg. Frachtkosten from my Datatable loaded to my Datagridview

    the Function..
    Code:
     Public Function Mittelwert(ByVal TB As DataTable) As Single
            Dim SumVal As Single = 0
            For Each Rw As DataRow In TB.Rows
                SumVal += Convert.ToSingle(Rw("Frachtkosten"))
            Next
            Return SumVal / TB.Rows.Count
        End Function
    then to call it...
    Code:
     
    MsgBox(Mittelwert(objDataTable))
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] getting an average in a datagridview

    Quote Originally Posted by ChrisE View Post
    Hi,

    if you use a Datatable, you can use this...

    I want to get the avg. Frachtkosten from my Datatable loaded to my Datagridview

    the Function..
    Code:
     Public Function Mittelwert(ByVal TB As DataTable) As Single
            Dim SumVal As Single = 0
            For Each Rw As DataRow In TB.Rows
                SumVal += Convert.ToSingle(Rw("Frachtkosten"))
            Next
            Return SumVal / TB.Rows.Count
        End Function
    then to call it...
    Code:
     
    MsgBox(Mittelwert(objDataTable))
    regards
    Chris
    I would opt for this method, however I would build a condition to remove NULL values from the AVG. I am not sure what the default behavior is when getting average via aggregate avg function from sql or datacolumn expression, but I am thinking NULLS are not included in the results. I could be wrong

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width