Problème couleur de fond du DataGridView suite à l'event Scroll

New31000 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention   -  
 Utilisateur anonyme -
Bonjour tout le monde,

Je travaille sur la conception d'un chat dans le cadre d'un stage et j'ai un problème très bizarre.

Lorsque j'affiche les messages sur le chat, lors du premier chargement, voici ce que ça donne :



Jusque là, tout est parfait.
Le problème c'est que dès que j'ajoute un message, et que le DGV est rafraichi, ou bien dès que je scroll pour voir les messages plus haut, voici ce que j'obtiens :



Toutes les couleurs sont mélangées, et les cellules se chevauchent...

J'ai essayé ceci :


Private [/contents/446-fichier-sub Sub] dgv_chat_scroll(sender As Object, e As ScrollEventArgs) Handles dgv_chat.Scroll
 
        For i = 0 To dgv_chat.RowCount - 1
            dgv_chat.Rows(i).Cells(0).Style.BackColor = Color.Black
            dgv_chat.Rows(i).Cells(1).Style.BackColor = Color.Black
            dgv_chat.Rows(i).Cells(2).Style.BackColor = Color.Black
            dgv_chat.Rows(i).Cells(3).Style.BackColor = Color.Black
            dgv_chat.Rows(i).Cells(4).Style.BackColor = Color.Black
            dgv_chat.Rows(i).Cells(5).Style.BackColor = Color.Black
        Next
 
        For i = 0 To dgv_chat.RowCount - 1
            If dgv_chat.Rows(i).Cells(2).Value = "" Then
                dgv_chat.Rows(i).Cells(2).Style.BackColor = Color.Transparent
                dgv_chat.Rows(i).Cells(4).Style.BackColor = Color.LightBlue
            Else
                dgv_chat.Rows(i).Cells(2).Style.BackColor = Color.FromArgb(147, 182, 216)
                dgv_chat.Rows(i).Cells(4).Style.BackColor = Color.Transparent
            End If
        Next
 
    End Sub

Mais cela ne change rien.

Est-ce que quelqu'un aurait une idée par hasard?
Merci beaucoup ! :D

2 réponses

Utilisateur anonyme
 
Bonjour
Peux tu montrer le code qui donne le résultat avant le scroll?
0
New31000 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 

Public Sub remplissage_dgv_chat()

For i = 0 To Form_Chat.dgv_chat.RowCount - 1

Form_Chat.dgv_chat.Rows(i).Cells(0).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
Form_Chat.dgv_chat.Rows(i).Cells(1).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
Form_Chat.dgv_chat.Rows(i).Cells(2).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
Form_Chat.dgv_chat.Rows(i).Cells(3).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
Form_Chat.dgv_chat.Rows(i).Cells(4).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
Form_Chat.dgv_chat.Rows(i).Cells(5).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))

0
Utilisateur anonyme
 
Merci à Francis d'avoir édité ton premier message qui est bien plus lisible maintenant.

Voir ici https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code , pour corriger ton second message s'il te plait
0
New31000 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 
<code>
Public Sub remplissage_dgv_chat()

        For i = 0 To Form_Chat.dgv_chat.RowCount - 1

            Form_Chat.dgv_chat.Rows(i).Cells(0).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
            Form_Chat.dgv_chat.Rows(i).Cells(1).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
            Form_Chat.dgv_chat.Rows(i).Cells(2).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
            Form_Chat.dgv_chat.Rows(i).Cells(3).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
            Form_Chat.dgv_chat.Rows(i).Cells(4).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))
            Form_Chat.dgv_chat.Rows(i).Cells(5).Style.BackColor = Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))


</code>
0
Utilisateur anonyme
 
OK,

parlons déjà de ça
Color.FromArgb(toInteger(CType(Couleur_Logiciel_R, String)), toInteger(CType(Couleur_Logiciel_V, String)), toInteger(CType(Couleur_Logiciel_B, String)))

tu fais une succession de conversions et de cast. Ça prend du temps d'exécution, des ressources et du temps d'écriture.
Si au load, ou après avoir affecté tes variables "couleur logiciel", tu faisais une fois pour toute cette conversion, en la stockant dans une variable ça serait mieux.

En plus là tu as 6 fois la même ligne, y'a juste un chiffre qui change, une boucle For serait appropriée.

Mais il me semble (je ne fais plus de winform depuis un moment) qu'on peut affecter le background d'une ligne entière.
https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.forms.datagridviewrow.defaultcellstyle?view=netframework-4.7.2#System_Windows_Forms_DataGridViewRow_DefaultCellStyle

Cela dit, ça ne répond pas à ma question, je t'ai demandé comment ton datagridview est rempli, pas comment il est coloré.

0
New31000 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 
En fait, c'est pas un problème de remplissage du DataGridView.
Le problème c'est pendant que je scroll, ça me mélange toutes les cellules du chat ainsi que les couleurs.
Merci
0
Utilisateur anonyme
 
Peut être, mais je ne peux pas (et personne d’ailleurs) tester chez moi ce qui se passe par magie.
0