lunes, 7 de diciembre de 2015

Promedios



Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        grilla()

    End Sub

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


        Dim dt As New DataTable
        Dim da As New Data.SqlClient.SqlDataAdapter

       
        da.SelectCommand = New SqlClient.SqlCommand
        da.SelectCommand.Connection = New SqlClient.SqlConnection(My.Resources.conn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.CommandText = ("golf")
        da.Fill(dt)
        Dim promedio As Integer = 0
        Dim n As Integer = 0
        Dim suma As Integer = 0
        Dim dni As Integer = 0
        Dim fila As Integer = 0
        Dim p As Integer = 0
        Dim socio1 As Integer = 33
        Dim socio2 As Integer
        Dim socio3 As Integer
        Dim cuenta As Integer
        Dim parcial As Integer


        Dim dtf As New DataTable()

        dtf.Columns.AddRange(New DataColumn() {New DataColumn("Id_Socio", GetType(Integer)), New DataColumn("Promedio", GetType(Integer))})



        For i = 0 To dt.Rows.Count - 1
            socio3 = dt.Rows(i).Item("socio")

            'Comparo el Nro de Socio cargado antes de entrar con el actual.
            'Como son distintos va a entrar

            If socio1 <> socio3 Then
                socio1 = dt.Rows(i).Item("socio")

                'El segundo For recorre comenzando desde el valor de la posición actual del primero
                For z = i To dt.Rows.Count - 1
                    socio2 = dt.Rows(z).Item("socio")

                    'Si son iguales opero
                    If socio1 = socio2 Then

                        'Acumuladores

                        If dt.Rows(z).Item("Par_cancha") < dt.Rows(z).Item("Golpes") Then
                            parcial = dt.Rows(z).Item("Golpes") - dt.Rows(z).Item("Par_cancha")
                            suma = suma + parcial
                            cuenta = cuenta + 1
                        End If

                        If dt.Rows(z).Item("Par_cancha") > dt.Rows(z).Item("Golpes") Then
                            parcial = dt.Rows(z).Item("Par_cancha") - dt.Rows(z).Item("Golpes")
                            suma = suma + parcial
                            cuenta = cuenta + 1
                        End If

                        'Si no son iguales es que cambio el socio
                    Else
                        'Calculo el promedio
                        promedio = suma / cuenta

                        'Asigno a la DT
                        dtf.Rows.Add(socio1, promedio)

                        'Blaqueo variables
                        suma = 0
                        cuenta = 0
                        promedio = 0

                        Exit For
                    End If
                Next
            End If
        Next

        'Esto lo calculo y lo muestro para el último
        promedio = suma / cuenta
        dtf.Rows.Add(socio1, promedio)


        'Asigno valores a la DT final
        GridView2.DataSource = dtf
        GridView2.DataBind()

    End Sub


    Public Sub grilla()

        Dim dt As New DataTable
        Dim da As New Data.SqlClient.SqlDataAdapter


        da.SelectCommand = New SqlClient.SqlCommand
        da.SelectCommand.Connection = New SqlClient.SqlConnection(My.Resources.Conn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        da.SelectCommand.CommandText = ("golf")
        da.Fill(dt)

        GridView1.DataSource = dt
        GridView1.DataBind()


    End Sub

End Class

No hay comentarios: