Para el ejemplo colocar en un formulario dos controles Button, un control Label y un Datagridview
Código fuente
Option Explicit On
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Sumar columnas en DataGridview"
Button1.Text = "Sumar"
Button2.Text = "Cargar valores"
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
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' muostrar el total de la suma en el control Label para la columna llamada Costo
Label1.Text = "Total :" & Format(Sumar("costo", DataGridView1), "c").ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Randomize()
With DataGridView1
' Agregar dos columnas
.Columns.Clear()
.Columns.Add("Id", "Id Producto")
.Columns.Add("Costo", "Costo")
' agregar 10 filas
.RowCount = 10
' añadir un valor para el campo ID
For Filas As Integer = 0 To .RowCount - 1
.Item(0, Filas).Value = Filas
Next
' añadir un valor aleatorio para el campo Costo
For Filas As Integer = 0 To .RowCount - 1
.Item(1, Filas).Value = Format(CInt(Rnd() * 2500), "c")
Next
End With
End Sub
End Class
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Sumar columnas en DataGridview"
Button1.Text = "Sumar"
Button2.Text = "Cargar valores"
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
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' muostrar el total de la suma en el control Label para la columna llamada Costo
Label1.Text = "Total :" & Format(Sumar("costo", DataGridView1), "c").ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Randomize()
With DataGridView1
' Agregar dos columnas
.Columns.Clear()
.Columns.Add("Id", "Id Producto")
.Columns.Add("Costo", "Costo")
' agregar 10 filas
.RowCount = 10
' añadir un valor para el campo ID
For Filas As Integer = 0 To .RowCount - 1
.Item(0, Filas).Value = Filas
Next
' añadir un valor aleatorio para el campo Costo
For Filas As Integer = 0 To .RowCount - 1
.Item(1, Filas).Value = Format(CInt(Rnd() * 2500), "c")
Next
End With
End Sub
End Class
No hay comentarios:
Publicar un comentario