0

I'm trying to calculate the standard deviation from data in a grid, but sth goes wrong.
I have tried with variable declarations inside and outside the function and the result stays the same.
It seems to me that the loop doesn't work properly.
Can you take a look at the "desvest" function at the end of the code.
Thank you!
(the name of the column of the datagridview is "Mediciones")

Public Class Form1
Dim totalp As Double
Dim mediap As Double
Dim cuentap As Single

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

totalp = Sumar("Mediciones", dgv1)
lblSuma.Text = "Suma :" & Format(totalp, "f").ToString
cuentap = Contar("Mediciones", dgv1)
lblCuenta.Text = "Cuenta :" & Format(cuentap, "").ToString
mediap = totalp / cuentap
lblMedia.Text = "Media :" & Format(mediap, "f").ToString
lblDesvest.Text = "Desviación Típica :" & Format(Desvest("Mediciones", dgv1), "f").ToString
End Sub
' función que retorna el total
Private Function Sumar( _
ByVal nombre_Columna As String, _
ByVal Dgv As DataGridView) As Double

Dim total As Double = 0

' recorrer las filas y obtener los items de la columna indicada en "nombre_Columna"
Try
For i As Integer = 0 To Dgv.RowCount - 1
total = total + CDbl(Dgv.Item(nombre_Columna.ToLower, i).Value)
Next

Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

' retornar el valor
Return total

End Function
' función que retorna la cuenta
Private Function Contar( _
ByVal nombre_Columna As String, _
ByVal Dgv As DataGridView) As Double

Dim cuenta As Single = 0

cuenta = Dgv.RowCount - 1

' retorna el valor
Return cuenta

End Function
' función que retorna la desviación standard
Private Function Desvest( _
ByVal nombre_Columna As String, _
ByVal Dgv As DataGridView) As Double

Dim SumSqdDif As Double
Dim respuesta As Double


' recorrer las filas y obtener los items de la columna indicada en "nombre_Columna"
Try
For i As Integer = 0 To Dgv.RowCount - 1

SumSqdDif = SumSqdDif + (CDbl(Dgv.Item(nombre_Columna.ToLower, i).Value) - mediap) ^ 2
Next
respuesta = Math.Sqrt(SumSqdDif / (cuentap - 1))


Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try

' retornar el valor
Return respuesta

End Function
End Class

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by solfinker
0

I've got it!
The datagridview always counts on the last cell. So the loop must be till count-2

Edited by solfinker: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.