You are on page 1of 2

Option Explicit On

Option Strict On
Imports System.Data
Imports System.Data.SqlClient

Public Class Form23


Inherits System.Windows.Forms.Form
Private oConexion As SqlConnection
Private oDataSet As DataSet
Private oDataAdapter As SqlDataAdapter

Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles GroupBox1.Enter

End Sub

Private Sub grddatos_CellContentClick(ByVal sender As


System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
grddatos.CellContentClick

End Sub

Private Sub Form23_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

oConexion = New SqlConnection()


oConexion.ConnectionString =
"Server=.\SQLEXPRESS;Database=bdtesoreria;Integrated Security=True;"
oDataAdapter = New SqlDataAdapter()

Dim oCmdConsulta As New SqlCommand("SELECT * FROM Recibos ",


oConexion)
'Dim oCmdConsulta As New SqlCommand("SELECT N_Partida,total
FROM where N_Partida like'1212421' ", oConexion)
oDataAdapter.SelectCommand = oCmdConsulta
oDataSet = New DataSet()
oDataSet.Clear()
oConexion.Open()
oDataAdapter.Fill(oDataSet, "Recibos")
oConexion.Close()
Me.grddatos.DataSource = oDataSet
Me.grddatos.DataMember = "Recibos"

Me.Text = "Sumar columnas en DataGridview"


Calcular.Text = "Sumar"
'Button2.Text = "Cargar valores"
End Sub

' funcin 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

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles calcular.Click
' muostrar el total de la suma en el control Label para la
columna llamada Costo
Label1.Text = " total :" & Format(Sumar("total", grddatos),
"c").ToString
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles Button1.Click

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles TextBox1.TextChanged
oConexion = New SqlConnection()
oConexion.ConnectionString =
"Server=.\SQLEXPRESS;Database=bdtesoreria;Integrated Security=True;"
oDataAdapter = New SqlDataAdapter()
Dim oCmdConsulta As New SqlCommand("SELECT N_Partida, Total
FROM Recibos WHERE N_Partida like '" & Me.TextBox1.Text & "'",
oConexion)
oDataAdapter.SelectCommand = oCmdConsulta
oDataSet = New DataSet()
oDataSet.Clear()
oConexion.Open()
oDataAdapter.Fill(oDataSet, "Recibos")
oConexion.Close()
Me.grddatos.DataSource = oDataSet
Me.grddatos.DataMember = "Recibos"
End Sub
End Class