La Web del Programador: Comunidad de Programadores
 
    Pregunta:  64615 - RECORRER GRIDVIEW Y CAMBIAR COLOES DE CELDAS
Autor:  Gustavo
hola mi problema es que en un gridview donde inserto el porcentaje propuesto de un pryecto y el avance real, y quiero que si el avance real es menor al prpuesto cambie de color la celda (o se su defecto el texto) espero que alguien me pueda ayudar Gracias!!!

  Respuesta:  abel montalvo
Hola compaƱero
Espero y te sirva
un saludo
Agrega un DataGridView as un Form en un proyecto nuevo y pega esto

Public Class Form1

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

Try
Dim AvanceReal As Integer = Integer.Parse(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
Dim AvancePropuesto As Integer = 80
If AvanceReal < AvancePropuesto Then
Dim CellStyle As DataGridViewCellStyle = New DataGridViewCellStyle
CellStyle.BackColor = Color.Red
CellStyle.ForeColor = Color.Blue
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style = CellStyle
Else
Dim CellStyle As DataGridViewCellStyle = New DataGridViewCellStyle
CellStyle.BackColor = Color.Green
CellStyle.ForeColor = Color.Black
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style = CellStyle
End If

Catch ex As Exception
'Entrara en dado caso que la celda =""
Dim CellStyle As DataGridViewCellStyle = New DataGridViewCellStyle
CellStyle.BackColor = Color.White
CellStyle.ForeColor = Color.Black
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style = CellStyle
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet = New DataSet("DS")
Dim dt As DataTable = New DataTable("dt")
dt.Columns.Add(New DataColumn("AvanceReal", GetType(String)))
dt.Columns.Add(New DataColumn("AvancePropuesto", GetType(String)))
dt.Rows.Add("", 90)
dt.Rows.Add("", 80)
dt.Rows.Add("", 70)
dt.Rows.Add("", 50)
'ds.Tables.Add(dt)
DataGridView1.DataSource = dt



DataGridView1.Columns(1).ReadOnly = True

End Sub
End Class