La mayoría de los proyecto realizado, siempre utilizamos los mismos controles de siempre en esta oportunidad tenemos un ejemplo de como personalizas la filas de un datagridview.
El siguiente ejemplo nos muestra la siguiente imagen donde observamos que las columnas ("código"," producto", "precio * unidad") tienen las primeras fila un BackColor = Write , pero la ultima se asigno un BackColor = PaleGreen, de misma manera la columnas de ("Cant")., tiene las primeras filas un BackColor = LemonChiffon pero la ultima fila tiene el color asignado BackColor =MistyRose.
Así mismo estamos utilizando el evento CellEndEdit del datagridview, que nos permitirá realizar los cálculos del importe a pagar luego de haber finalizado la edición de la celda ("Cant").
Código fuente
Paso 1: Programar función Listar_productos
Sub Listar_productos()
Try
Dim Codigo() As Integer = {1, 2, 3, 4, 5}
Dim Productos() As String = {"Gaseosa", "Yogurt", "Galletas", "Chocolates", "Caramelos"}
Dim PreciosXunidad() As Double = {1.5, 1.2, 0.5, 0.5, 0.1}
DataGridView1.Rows.Clear()
For index As Integer = 0 To Codigo.Length - 1
DataGridView1.Rows.Add()
DataGridView1("txtCodigo", index).Value = Codigo(index)
DataGridView1("txtProducto", index).Value = Productos(index)
DataGridView1("txtPrecioXunidad", index).Value = PreciosXunidad(index)
Next
'Agregamos una fila para que contendrá lo siguiente
DataGridView1.Rows.Add()
DataGridView1("txtProducto", DataGridView1.Rows.Count - 1).Value = "Importe Total Pagar S/."
DataGridView1("txtPrecioXunidad", DataGridView1.Rows.Count - 1).Value = "===>"
Catch ex As Exception
MessageBox.Show(ex.Message, "vb.net", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Paso 2: Cargar los productos
Private Sub frmVentas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Cargamos los productos
Listar_productos()
End Sub
Paso 3: Asignamos formatos a las celda del datagridview
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Try
Dim Tipo As String
'comprobamos que sea la columna txtCant
If (DataGridView1.Columns(e.ColumnIndex).Name.Equals("txtCant")) Then
'Capturamos el valor de la celda
Tipo = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
If (DataGridView1.Rows.Count - 1 > e.RowIndex) Then
If (Tipo = "") Then
e.CellStyle.BackColor = Color.LemonChiffon
Else
e.CellStyle.BackColor = Color.White
End If
Else
e.CellStyle.BackColor = Color.MistyRose
End If
ElseIf (DataGridView1.Columns(e.ColumnIndex).Name.Equals("txtProducto")) Then
Tipo = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
'verificamos que el valor de la celda contemga el siguiente Texto
If (Tipo = "Importe Total Pagar S/.") Then
'asignamos un color que diferencie a las demas celdas de la columna txtProducto
DataGridView1("txtCodigo", e.RowIndex).Style.BackColor = Color.PaleGreen
DataGridView1("txtProducto", e.RowIndex).Style.BackColor = Color.PaleGreen
DataGridView1("txtPrecioXunidad", e.RowIndex).Style.BackColor = Color.PaleGreen
Else
e.CellStyle.BackColor = Color.White
End If
ElseIf (DataGridView1.Columns(e.ColumnIndex).Name.Equals("txtImportePagar")) Then
Tipo = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
If (DataGridView1.Rows.Count - 1 > e.RowIndex) Then
If (Tipo = "") Then
e.CellStyle.BackColor = Color.White
Else
e.CellStyle.BackColor = Color.Silver
End If
Else
e.CellStyle.BackColor = Color.MistyRose
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "vb.net", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Paso 4: Programar CellEndEdit
Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Try
'verifico que la columna seleccionad sea la de txtcant
If (e.ColumnIndex = 3) Then
'capturamos el precio y la cantidad ingresada de la fila actual
Dim PrecioUnitario As Double = DataGridView1("txtPrecioXunidad", e.RowIndex).Value
Dim Cantidad As Integer = DataGridView1("txtCant", e.RowIndex).Value
'Calculamos el importe a pagar
Dim ImportePagar As Double = PrecioUnitario * Cantidad
'Asignamos el importe a pagar a la fila actual
DataGridView1("txtImportePagar", e.RowIndex).Value = ImportePagar
'ahora tendremos que calcular el total a apagar y las cantidades a vender
'le descuento filas porque una de ellas es la fila que dice // Importe Total Pagar S/.
Dim CantVentas As Integer = 0
Dim ImporteTotal As Double = 0
For rows As Integer = 0 To DataGridView1.Rows.Count - 2
CantVentas += DataGridView1("txtCant", rows).Value
ImporteTotal += DataGridView1("txtImportePagar", rows).Value
Next
'ahora asignare los valores a la fila // Importe Total Pagar S/.
DataGridView1("txtCant", DataGridView1.Rows.Count - 1).Value = CantVentas
DataGridView1("txtImportePagar", DataGridView1.Rows.Count - 1).Value = ImporteTotal
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "vb.net", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Resultado

Como podemos apreciar se ha calculado el importe a pagar y a la vez estamos asignando la fila "Importe Total Pagar S/." , la cantidad total vendida y el importe total a pagar.
No hay comentarios:
Publicar un comentario