• Miércoles 15 de Mayo de 2024, 00:40

Autor Tema:  COMO EVALUAR DATOS DE UN DATAGRIDVIEW EN UNA FUNCION MATEMAT  (Leído 1998 veces)

gjcfm2

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
COMO EVALUAR DATOS DE UN DATAGRIDVIEW EN UNA FUNCION MATEMAT
« en: Jueves 7 de Octubre de 2010, 05:56 »
0
Buenas noches.
Estoy realizando una aplicación en VB.NET 2010 EXPRESS

Agradeceré su ayuda para lo siguiente:
Tengo una Datagridview1, el cual se carga con datos procedentes de una hoja de Excel.
Los datos enviados se almacenan en dos columnas (MD Y TVD) del Datagridview con los características siguientes:
      A         B         C
     MD        TVD      DESV
 0     0         0        
 1    30.48    30.48 .....DESV1
 2    60.96    60.96......DESV2
 3    91.44    91.44......DESV3
 4    121.92   121.92
 5    152.4    152.4
 6    182.88   182.88
 7    213.36   213.36
 8    243.84   243.84
 9    274.32   274.32
 10  304.8     304.8
 11  335.28   335.28
 12  365.76   365.76
 13  396.24   396.24
 14  426.72   426.72
 15  457.2     457.2
 16  487.68   487.68
 17  1066.8   1066.75
 18  1097.28  1097.23
 19  1114      1113.95
 20  1118      1117.95
 21  1123      1122.95
 22  1132.3   1132.23
 23  1141.5   1141.4
 24  1150.3   1150.16 .... DESV 24

CON LOS DATOS ANTERIORES REQUIERO PUEDAN AYUDARME CON ALGÚN CODIGO EN VB.NET PARA PODER EVALUAR LOS VALORES ANTERIORES
EN LA SIGUIENTE FUNCIÓN:

DESV = COS ((A1-A0)/(B1-B0))/3.1416*180

PARA PODER DAR UNA MEJOR DESCRIPCION DE LA FUNCIÓN QUE REUQIERO EVALUAR los numeros del 0---24, representan las filas.
A Y B LAS COLUMNAS COMO EN EXCEL. ENTONCES LA FUNCIÓN SERIA.

DESV = COS ((A1-A0)/(B1-B0))/3.1416*180
OSEA   COS ((30.48-0)/(30.48-0)/3.1416*180
           
          ((A2-A1)/(B2-B1))/3.1416*180
          ((60.96-30.48)/(60.96-30.48))/3.1416*180

          ((A3-A1)/(B3-B1))/3.1416*180
              .       .´        .
              .       .         .
              ETC...

DONDE

D.ONDE COS= FUNCION TRINOMETRICA

LOS VALORES CALCULADOS POR CADA FILA O CELDA DEBEN SER PUESTO EN LA COLUMNA "DESV"
COMO SE MUESTRA ARRIBA


EL CODIGO CON EL QUE CARGO DATOS DE EXCEL AL DATAGRIDVIEW ES EL SIGUIENTE.


Option Explicit On
Imports System.Data.OleDb
Public Class Fohijo1

    Dim dt As New DataTable

    Private Sub Fohijo1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub ButtonFohijo1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFohijo1.Click

        Dim openFD As New OpenFileDialog()
        With openFD
            .Title = "Seleccionar archivos"
            .Filter = "Archivos Excel(*.xls;*.xlsx)|*.xls;*.xlsx|Todos los archivos (*.*)|*.*"
            .Multiselect = False
            .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                TextPathExcel.Text = .FileName
            End If

        End With

    End Sub

    Private Sub ButtonFohijo1a_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFohijo1a.Click

        Try
            Dim stConexion As String
            stConexion = ("Provider=Microsoft.ACE.OLEDB.12.0;" & ("Data Source=" & (TextPathExcel.Text & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=2"";")))
            Dim cnConex As New OleDbConnection(stConexion)
            Dim Cmd As New OleDbCommand("Select * from [" & TextHoja.Text & "$]")
            Dim Ds As New DataSet
            Dim Da As New OleDbDataAdapter
            Dim Dt As New DataTable
            cnConex.Open()
            Cmd.Connection = cnConex
            Da.SelectCommand = Cmd
            Da.Fill(Ds)
            Dt = Ds.Tables(0)
            DataGridView1.DataSource = Dt
            cnConex.Close()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        End Try


    End Sub

    Private Sub ButtonFohijo1b_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFohijo1b.Click

        With DataGridView1

            .DataSource = Nothing

        End With

    End Sub


End Class

LuisYactayo

  • Miembro activo
  • **
  • Mensajes: 36
  • Nacionalidad: pe
    • Ver Perfil
Re: COMO EVALUAR DATOS DE UN DATAGRIDVIEW EN UNA FUNCION MATEMAT
« Respuesta #1 en: Lunes 18 de Octubre de 2010, 18:06 »
0
Quizas el codigo que te paso te ayude. He llenado las dos primeras columnas en un datagrid en el evento load con datos aleatorios.
Luego llené la tercera columna con la formula que pasaste. Solo coloca en un form un datagridview con nombre = DGV y copia el codigo q te paso en el evento load.   Quizas lo puedes colocar en el momento que se cargan los datos en el datagridview.
Cualquier explicacion mas me escribes
Código: vb.net
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         'Asignándole 24 filas al datagridview:
  4.         DGV.RowCount = 24
  5.         'Asignándole 3 columnas al datagridview:
  6.         DGV.ColumnCount = 3
  7.         'Colocando el nombre de las columnas:
  8.         DGV.Columns(0).HeaderText = "MD"
  9.         DGV.Columns(1).HeaderText = "TVD"
  10.         DGV.Columns(2).HeaderText = "DESV"
  11.         Randomize() ' Inicializando los numeros aleatorios
  12.         'Llenando la primera fila con ceros:
  13.         DGV.Item(0, 0).Value = 0
  14.         DGV.Item(1, 0).Value = 0
  15.         DGV.Item(2, 0).Value = 0
  16.         'El for recorre todas las filas del datagridview
  17.         For F As Integer = 1 To DGV.RowCount - 1
  18.             'Llenando las dos primeras columnas con numeros aletorios
  19.             DGV.Item(0, F).Value = Rnd() * 100
  20.             DGV.Item(1, F).Value = Rnd() * 100
  21.             'AQUI ESTA LO PRINCIPAL:
  22.             Dim DEV As Double
  23.             DEV = Math.Cos((DGV.Item(0, F).Value - DGV.Item(0, F - 1).Value) / _
  24.                            (DGV.Item(1, F).Value - DGV.Item(1, F - 1).Value)) / Math.PI * 180
  25.             'MATH.COS --> devuelve el coseno
  26.             'MATH.PI  --> devuelve el valor de Pi
  27.             DGV.Item(2, F).Value = DEV 'Agregando  el DEV a la tercera columna de la fila actual
  28.         Next
  29.     End Sub
  30. End Class
  31.