Results 1 to 4 of 4

Thread: [RESOLVED] Datagridview Average

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    71

    Resolved [RESOLVED] Datagridview Average

    This one should be fairly easy (I hope)...

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim tot As Integer = 0
            For i As Integer = 0 To DataGridView1.RowCount - 1
                If DataGridView1.Rows(i).Cells(1).Value = txtName.Text Then
                    tot += DataGridView1.Rows(i).Cells(5).Value
                End If
            Next
            If tot = 0 Then
                MsgBox("No Records Found")
            End If
            txtTotal.Text = tot
        End Sub
    What I need help with is how to expand this code to have it average the total sum this code calculates by the number of entries it added. Basically I need it to count how many different entries it added to get that total and use that number to average.

    You guys have been a big help so far, thanks and I hope I can get along on my own for a while once this is done!

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

    Re: Datagridview Average

    First up, why make your code less clear by using "tot" instead of "total" just to save yourself two keystrokes? If the extra two characters is too much work then why not use "sum" instead?

    As for the question, you already know how to do it because you're already basically doing it. In your code, you declare 'tot', initialise it to zero and then add a value to it inside the loop. You can do exactly the same thing to calculate a count. The only difference would be that you would add 1 to it inside the loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    71

    Re: Datagridview Average

    As for "tot" instead of "total", I'm just learning and I found that in another loop forum. Makes sense, I'll try to start using better practices. I am at the point right now when I read someone else's code it makes sense and I can usually repeat it once I've done it, but just coming up with it on the spot I usually get it about half right and spend hours of frustration and reading forums trying everything but the right way... then when I finally get it to do what I want, I find out there's an easy way to make an error that I now have to code against

    I don't know if it's "correct" but it works... if there's a better/safer way please let me know.

    Code:
    Dim total As Integer = 0
            Dim total2 As Integer = 0
            For i As Integer = 0 To DataDataGridView.RowCount - 1
                If IsNumeric(DataDataGridView.Rows(i).Cells(1).Value) Then
                    If Not IsDBNull(DataDataGridView.Rows(i).Cells(6).Value) Then
                        If DataDataGridView.Rows(i).Cells(1).Value = txtName.text Then
                            total += DataDataGridView.Rows(i).Cells(6).Value
                            total2 += 1
                        End If
                    End If
                End If
    
            Next
            If total2 > 0 Then
                Dim text As Integer
                text = total / total2
                Label1.Text = text
            End If
    
            If Val(Label1.Text) < Val(TextBox3.Text) Then
                With Label1
                    .ForeColor = Color.Red
                End With
            Else : Label1.ForeColor = Color.Black
            End If
    Last edited by jpiper; Mar 5th, 2011 at 11:25 PM. Reason: total2 += Me.DataDataGridView.RowCount / Me.DataDataGridView.RowCount does = 1

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

    Re: Datagridview Average

    Ah, I see you edited your post after I first looked at it. It seems better, but 'total2' is a terrible variable name. You should use names that indicate the purpose. It's a count so call it 'count' or something more specific.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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