Foros aprenderaprogramar.com
Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: Mary en 24 de Septiembre 2014, 20:29
-
Hola,
sigo haciendo una aplicación con VB 2010 y tengo la siguiente duda que no se ni como empezar. Bueno, mi problema es el siguiente:
Tengo un datagridview con la fila de encabezado y tres filas más tal como muestro a continuación:
VALOR1 VALOR2 VALOR3 VALOR4 VALOR5
2 4 5.5 0.6 7.8
1.2 2.3 8.6 1.2 5.3
0.5 3.8 1.2 0.5 2.3
Las tres filas contienen números con decimales. Lo que intento es que a través de un button se puede conseguir los siguiente:
Si en la columna VALOR1, el número de la fila 2 (en este caso 1.2) sea mayor que el valor de la fila 1 (2) o menor que el valor de la fila 3 (0.5), el número 1.2 aparezca de color rojo. y si está dentro del intervalo, se mantenga de color negro. Y hacer esto mismo con los valores de las cinco columnas.
Espero que alguien sepa como hacerlo o quizás darme alguna idea de como debería hacerse. Gracias y saludos.
-
La idea natural parece que cuando detectes el evento de presión del button compruebes los valores y cambies las propiedades que tengas que cambiar. Fíjate en este ejemplo cómo recorren las celdas del datagridview y modifican propiedades: https://www.aprenderaprogramar.com/foros/index.php?topic=1029
Salu2
-
Hola Ogramar,
leí el link que me pasaste y pude sacar información para conseguir lo que pretendía. He estado haciendo pruebas para intentar hacer un bucle For next para todas las columnas que tiene mi datagridview, pero no he podido conseguirlo. Así que lo que he hecho es una comparación para cada una de las ocho columnas que tengo. Queda un poco mal, pero me sirve. A continuación pongo el código que he conseguido.
Saludos y gracias. El tema se pude cerrar.
Private Sub comparar()
If (DataGridView32.Item("Column1", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column1", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column1", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column1", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(0).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(0).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column2", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column2", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column2", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column2", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(1).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(1).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column3", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column3", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column3", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column3", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(2).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(2).Style.ForeColor = Color.Black
End If
' DataGridView32.ClearSelection()
If (DataGridView32.Item("Column4", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column4", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column4", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column4", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(3).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(3).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column5", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column5", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column5", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column5", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(4).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(4).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column6", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column6", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column6", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column6", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(5).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(5).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column7", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column7", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column7", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column7", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(6).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(6).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column8", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column8", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column8", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column8", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(7).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(7).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column9", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column9", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column9", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column9", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(8).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(8).Style.ForeColor = Color.Black
End If
If (DataGridView32.Item("Column10", DataGridView32.Rows(1).Index).Value) > (DataGridView32.Item("Column10", DataGridView32.Rows(0).Index).Value) Or (DataGridView32.Item("Column10", DataGridView32.Rows(1).Index).Value) < (DataGridView32.Item("Column10", DataGridView32.Rows(2).Index).Value) Then
DataGridView32.Rows(1).Cells(9).Style.ForeColor = Color.Red
Else
DataGridView32.Rows(1).Cells(9).Style.ForeColor = Color.Black
End If
End Sub
-
Gracias a tí por compartir el código, quizás le sea útil a otras personas. Salu2