Color en fila de Datagridview en VbNet
Hace mucho tiempo que no hecho otro post de programación , y me alegra volver. Como dice el encabezado, vamos a colocar un color determinado a una fila o Row de un datagridview. Es importante en situaciones donde necesitamos detectar un desbalance en valores calculados y que mejor si se presenta mediante un color que hacerlo con un mensaje, esto se pone evidente cuando existen muchos registros y la información se vuelve monótona.
Public Class Form1
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtcant.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txtbruto_GotFocus(sender As Object, e As EventArgs) Handles txtbruto.GotFocus
txtbruto.SelectAll()
End Sub
Private Sub txtbruto_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtbruto.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txttara_GotFocus(sender As Object, e As EventArgs) Handles txttara.GotFocus
txttara.SelectAll()
End Sub
Private Sub txttara_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txttara.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If txtcant.Text = "" Or txtbruto.Text = "" Then
MsgBox("Error al calcular. Faltan datos para efectuar", vbCritical)
Else
End If
grid.Rows.Add(1)
Dim cant, res As Double
Dim f As Integer = grid.Rows.Count - 1
Dim kilos As Double
kilos = Val(txtarroba.Text) / 4.4
kilos = Format(kilos, "0.000")
grid.Item(0, f).Value = f + 1
grid.Item(1, f).Value = Val(txtcant.Text) + kilos
grid.Item(2, f).Value = Val(txtbruto.Text)
grid.Item(3, f).Value = Val(txttara.Text)
grid.Item(4, f).Value = (grid.Item(2, f).Value - grid.Item(3, f).Value) / 50
res = grid.Item(4, f).Value
cant = grid.Item(1, f).Value
If res > cant Then
grid.Rows(f).DefaultCellStyle.BackColor = Color.GreenYellow
End If
txtcant.Focus()
txtcant.SelectAll()
Catch ex As Exception
MsgBox("Error al calcular, vbCritical")
End Try
End Sub
Private Sub txtarroba_GotFocus(sender As Object, e As EventArgs) Handles txtarroba.GotFocus
txtarroba.SelectAll()
End Sub
Private Sub txtarroba_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtarroba.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txtarroba_TextChanged(sender As Object, e As EventArgs) Handles txtarroba.TextChanged
End Sub
Private Sub btnexcel_Click(sender As Object, e As EventArgs) Handles btnexcel.Click
'Crer dataset para exportar
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'agregar columnas a la tabla
For i As Integer = 0 To grid.ColumnCount - 1
dset.Tables(0).Columns.Add(grid.Columns(i).HeaderText)
Next
'agregar filas a la tabla
Dim dr1 As DataRow
For i As Integer = 0 To grid.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To grid.Columns.Count - 1
dr1(j) = grid.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
wSheet.Name = "Ejemplo"
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim blnFileOpen As Boolean = False
Try
Catch ex As Exception
blnFileOpen = False
End Try
excel.Visible = True
End Sub
Private Sub EliminarFilaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EliminarFilaToolStripMenuItem.Click
grid.Rows.Remove(grid.CurrentRow)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Bueno primer lugar una captura asociada a un ejemplo en particular que va a ser mi aplicación.
Primeramente en que consiste mi ejemplo: Cuando la primera columna("Cantidad") de la grid es menor que la calculada ("Sacos")entre la diferencia de la segunda("Peso bruto") y la tercera("Tara") esto* 50, se colorea de verde toda la fila..... les adjunto al final mi ejemplo en VB2012 para mayor comprension!!
Bueno el código completo:
Además puedesexportar a Excel, haciendo referencia la libreria Microsoft Excel Object Library ...
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtcant.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txtbruto_GotFocus(sender As Object, e As EventArgs) Handles txtbruto.GotFocus
txtbruto.SelectAll()
End Sub
Private Sub txtbruto_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtbruto.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txttara_GotFocus(sender As Object, e As EventArgs) Handles txttara.GotFocus
txttara.SelectAll()
End Sub
Private Sub txttara_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txttara.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If txtcant.Text = "" Or txtbruto.Text = "" Then
MsgBox("Error al calcular. Faltan datos para efectuar", vbCritical)
Else
End If
grid.Rows.Add(1)
Dim cant, res As Double
Dim f As Integer = grid.Rows.Count - 1
Dim kilos As Double
kilos = Val(txtarroba.Text) / 4.4
kilos = Format(kilos, "0.000")
grid.Item(0, f).Value = f + 1
grid.Item(1, f).Value = Val(txtcant.Text) + kilos
grid.Item(2, f).Value = Val(txtbruto.Text)
grid.Item(3, f).Value = Val(txttara.Text)
grid.Item(4, f).Value = (grid.Item(2, f).Value - grid.Item(3, f).Value) / 50
res = grid.Item(4, f).Value
cant = grid.Item(1, f).Value
If res > cant Then
grid.Rows(f).DefaultCellStyle.BackColor = Color.GreenYellow
End If
txtcant.Focus()
txtcant.SelectAll()
Catch ex As Exception
MsgBox("Error al calcular, vbCritical")
End Try
End Sub
Private Sub txtarroba_GotFocus(sender As Object, e As EventArgs) Handles txtarroba.GotFocus
txtarroba.SelectAll()
End Sub
Private Sub txtarroba_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtarroba.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
If e.KeyChar = (".") Then
e.Handled = True
SendKeys.Send(",")
End If
End Sub
Private Sub txtarroba_TextChanged(sender As Object, e As EventArgs) Handles txtarroba.TextChanged
End Sub
Private Sub btnexcel_Click(sender As Object, e As EventArgs) Handles btnexcel.Click
'Crer dataset para exportar
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'agregar columnas a la tabla
For i As Integer = 0 To grid.ColumnCount - 1
dset.Tables(0).Columns.Add(grid.Columns(i).HeaderText)
Next
'agregar filas a la tabla
Dim dr1 As DataRow
For i As Integer = 0 To grid.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To grid.Columns.Count - 1
dr1(j) = grid.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
wSheet.Name = "Ejemplo"
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim blnFileOpen As Boolean = False
Try
Catch ex As Exception
blnFileOpen = False
End Try
excel.Visible = True
End Sub
Private Sub EliminarFilaToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EliminarFilaToolStripMenuItem.Click
grid.Rows.Remove(grid.CurrentRow)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Se ve muy bueno el ejemplo pero esta en VS2012 y aun no lo tengo
ResponderEliminarpuedes descargarlo el VS2012 y el serial puedes pedir a mi correo
EliminarGracias por el aporte.
ResponderEliminar