jueves, 17 de octubre de 2013

Calcular Datos en un DataGridview

Public Class Form1

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim i As Integer = DataGridView1.Rows.Count
        DataGridView1.Rows.Add()
        DataGridView1.Rows(i).Cells(0).Value = CInt(txtCodigo.Text)
        DataGridView1.Rows(i).Cells(1).Value = CStr(txtDescripcion.Text)
        DataGridView1.Rows(i).Cells(2).Value = CInt(txtCantidad.Text)
        DataGridView1.Rows(i).Cells(3).Value = CDec(txtPrecio.Text)
        DataGridView1.Rows(i).Cells(4).Value = Math.Round((CInt(txtCantidad.Text) * CDec(txtPrecio.Text)) * 0.2, 2) 'Aplicando descuento el 20%
        DataGridView1.Rows(i).Cells(5).Value = Math.Round((CInt(txtCantidad.Text) * CDec(txtPrecio.Text)) + CDec(DataGridView1.Rows(i).Cells(4).Value), 2)
        DataGridView1.ClearSelection()
        MostrarTotal()
    End Sub

    Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click

        If DataGridView1.Rows.Count > 0 Then
            Dim i As Integer = DataGridView1.CurrentRow.Index
            If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Selected = False) Then
                MsgBox("Seleccione Fila a Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                DataGridView1.Rows.RemoveAt(i)
                MostrarTotal()
                MsgBox("Eliminado Correctamente", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Else
            MsgBox("No existen Elementos Ok.", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub MostrarTotal()
        Dim Total As Decimal
        For i = 0 To DataGridView1.Rows.Count - 1
            Total = Total + CDec(DataGridView1.Rows(i).Cells(5).Value)
        Next
        lblTotal.Text = CDec(Total)
    End Sub
End Class


No hay comentarios:

Publicar un comentario