IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

colorer les lignes d'un datagridview avec RowPrePaint


Sujet :

VB.NET

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2007
    Messages
    758
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2007
    Messages : 758
    Points : 279
    Points
    279
    Par défaut colorer les lignes d'un datagridview avec RowPrePaint
    Bonjour,

    actuellement je colore les lignes de mon datagridview issu d'une base de donnée avec RowPrePaint, cela fonctionne bien ainsi je color de deux façon différente selon le texte qui se trouve dans la colonne n°2 qui vaut soit 'direct' soit 'multiple', voila le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
            Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
            Dim cellValue As Object = row.Cells(2).Value 'le numero 2 correspond au champ type
            If cellValue IsNot Nothing AndAlso cellValue.ToString.StartsWith("direct") Then
                row.DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf cellValue IsNot Nothing AndAlso cellValue.ToString.StartsWith("multiple") Then
                row.DefaultCellStyle.BackColor = Color.Tan
            End If
    End Sub
    Mais maintenant je souhaiterais aussi colorier les lignes au rouge qui contiennent la valeurs 1 dans un autre numéro de colonne (la N°11) ainsi voila mon code a présent :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
      Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
            Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
            Dim cellValue As Object = row.Cells(2).Value 'le numero 2 correspond au champ type
            If cellValue IsNot Nothing AndAlso cellValue.ToString.StartsWith("direct") Then
                row.DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf cellValue IsNot Nothing AndAlso cellValue.ToString.StartsWith("multiple") Then
                row.DefaultCellStyle.BackColor = Color.Tan
            End If
            Dim row2 As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
            Dim cellValue2 As Object = row2.Cells(11).Value 'le numero 11 correspond au champ statut
            If cellValue2 IsNot Nothing AndAlso cellValue.ToString.StartsWith("1") Then
                row2.DefaultCellStyle.BackColor = Color.Red
            End If
        End Sub
    Mais rien ne se passe, les lignes contenant 1 dans la colonne portant l'index n°11 ne passe pas au rouge, ainsi je me demande si l'on peu mettre dans RowPrePaint 2 conditions sur 2 colonne différentes ?

  2. #2
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    L'event CellFormatting est plus adapté à ton besoin que RowPrePaint.

    Dans L'event CellFormatting, on modifiera e.CellSyle en fonction de e.Value et éventuellement de e.RowIndex qui permettra de récupérer le contenu des autres champs de la row traitée.
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2007
    Messages
    758
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Février 2007
    Messages : 758
    Points : 279
    Points
    279
    Par défaut
    Merci bien pour ton conseil graffito j'ai utilisé le Cell Formating, voici mon code si cela peu servir un jour a quelqu'un :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            If TryCast(Me.DataGridView1.Rows(e.RowIndex).Cells("type").Value, String) = "direct" Then
                DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf TryCast(Me.DataGridView1.Rows(e.RowIndex).Cells("type").Value, String) = "multiple" Then
                DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Tan
            End If
            'on passe la ligne en rouge si on a un 1 dans statut
            If Me.DataGridView1.Rows(e.RowIndex).Cells("statut").Value = 1 Then
                DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
            End If
     
        End Sub

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Lire les lignes d'un fichier avec csh
    Par nicolas581 dans le forum Linux
    Réponses: 4
    Dernier message: 24/03/2010, 16h38
  2. Coloré les lignes d'un datagridview
    Par abbd dans le forum Windows Forms
    Réponses: 14
    Dernier message: 21/02/2008, 16h37
  3. [VB.NET]Comment supprimer les lignes d'un datagridView
    Par vijeo dans le forum Windows Forms
    Réponses: 1
    Dernier message: 01/09/2006, 17h54
  4. [VBA-E] Colorer les lignes sous-total
    Par steps5ive dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 08/07/2006, 18h47
  5. [VB 2005] Supprimer toutes les lignes d'un DataGridView
    Par Bob Langlade dans le forum Windows Forms
    Réponses: 4
    Dernier message: 25/01/2006, 16h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo