Meilleur auteur de réponses
Couleur des nombres dans les cellules d'une DataGridView

Question
-
Bonjour,
Quel est la chaine de format à utiliser dans la boîte de dialogue de Chaîne de format (Comportement/Format) pour changer la couleur du texte en fonction de la valeur du nombre contenu, (format personnaliser) ?
J'aimerai mettre les valeurs positives >0.5 en rouge et, si possible, en gras.
J'ai cherché dans la doc mais je n'ai trouvé que les formatages de présentation comme % mais pas la couleur.
Merci de votre aide.
MCh
- Modifié C Maurice mercredi 16 octobre 2013 15:31 Orthographe
Réponses
-
Bonjour,
En fait le mieux est d'utiliser les paramètres de l'événement qui contiennent l'indice la ligne et de la colonne dont la valeur a changé :
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles RR_RésultatDataGridView.CellValueChanged If RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value < 0 Then RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red Else RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Black End If End Sub
Cordialement
Gilles TOURREAU - MVP C#
Architecte logiciel/Consultant/Formateur Freelance - P.O.S Informatique
Blog : http://gilles.tourreau.fr - Suivez-moi sur Twitter
- MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
- MCSA : SQL Server 2012
- MCITP : SQL Server 2008 Developper
- MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0 / TFS 2010 / Windows Azure- Marqué comme réponse C Maurice jeudi 17 octobre 2013 11:22
-
Merci de votre aide éclairée.
Tout fonctionne très bien.
Voici le code final.
Private Sub Rouge(Table As DataGridView)
If Table.Rows.Count > 0 Then
Dim i As Integer = 0
For Each gRow As DataGridViewRow In Table.Rows
For i = 6 To 8
If gRow.Cells.Item(i).Value > 0.5 And gRow.Cells.Item(2).Value > 10 Then
gRow.Cells.Item(i).Style.ForeColor = Color.Red
Else
gRow.Cells.Item(i).Style.ForeColor = Color.Gray
End If
Next 'gRow
Next 'i
End If
End Sub
MCh
Toutes les réponses
-
-
J'ai essayé aussi sans succès :
Private Sub RR_RésultatDataGridView_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles RR_RésultatDataGridView.CellValueChanged If Not ini Then Exit Sub If RR_RésultatDataGridView.CurrentCell.Value < 0 Then RR_RésultatDataGridView.BackgroundColor = Color.Red Else RR_RésultatDataGridView.BackgroundColor = Color.Black End If End Sub End Class
MCh
-
Bonjour,
Essayez :
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles RR_RésultatDataGridView.CellValueChanged If RR_RésultatDataGridView.CurrentCell.Value < 0 Then RR_RésultatDataGridView.CurrentCell.Style.BackColor = Color.Red Else RR_RésultatDataGridView.CurrentCell.Style.BackColor = Color.Black End If End Sub
Cordialement
Gilles TOURREAU - MVP C#
Architecte logiciel/Consultant/Formateur Freelance - P.O.S Informatique
Blog : http://gilles.tourreau.fr - Suivez-moi sur Twitter
- MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
- MCSA : SQL Server 2012
- MCITP : SQL Server 2008 Developper
- MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0 / TFS 2010 / Windows Azure -
Bonjour et merci pour votre réponse.
J'ai une erreur :
Une exception non gérée du type 'System.InvalidOperationException' s'est produite dans Statistiques.exe
Informations supplémentaires : An error occurred creating the form. See Exception.InnerException for details. The error is: La référence d'objet n'est pas définie à une instance d'un objet.
Dans la Sub :
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _ Protected Overrides Sub OnCreateMainForm() Me.MainForm = Global.Statistiques.fStat End Sub
La valeur RR_RésultatDataGridView.CurrentCell.Value est Nothing.
Si je mets If IsNothing(RR_RésultatDataGridView.CurrentCell.Value) Then Exit Sub, j'ai la même erreur...
If IsNothing(RR_RésultatDataGridView.CurrentCell.Value) Then Exit Sub
MCh
- Modifié C Maurice jeudi 17 octobre 2013 10:05 erreur
-
-
Bonjour,
En fait le mieux est d'utiliser les paramètres de l'événement qui contiennent l'indice la ligne et de la colonne dont la valeur a changé :
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles RR_RésultatDataGridView.CellValueChanged If RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value < 0 Then RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red Else RR_RésultatDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Black End If End Sub
Cordialement
Gilles TOURREAU - MVP C#
Architecte logiciel/Consultant/Formateur Freelance - P.O.S Informatique
Blog : http://gilles.tourreau.fr - Suivez-moi sur Twitter
- MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
- MCSA : SQL Server 2012
- MCITP : SQL Server 2008 Developper
- MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0 / TFS 2010 / Windows Azure- Marqué comme réponse C Maurice jeudi 17 octobre 2013 11:22
-
Merci de votre aide éclairée.
Tout fonctionne très bien.
Voici le code final.
Private Sub Rouge(Table As DataGridView)
If Table.Rows.Count > 0 Then
Dim i As Integer = 0
For Each gRow As DataGridViewRow In Table.Rows
For i = 6 To 8
If gRow.Cells.Item(i).Value > 0.5 And gRow.Cells.Item(2).Value > 10 Then
gRow.Cells.Item(i).Style.ForeColor = Color.Red
Else
gRow.Cells.Item(i).Style.ForeColor = Color.Gray
End If
Next 'gRow
Next 'i
End If
End Sub
MCh