Lorsque je charge mon DGV, j'attribue différentes couleurs de backcolor en fonction de ma colonne (7). Cela charge parfaitement. Le problème est quand je clique sur n'importe quel en-tête pour trier la colonne, tout mon backcolar revient à "rien". Pourquoi? Tout ce que je fais est de cliquer sur l'en-tête pour trier la colonne, et je n'ai pas de code pour changer la couleur de fond en cliquant sur une cellule.datagridview Numéro d'en-tête de colonne
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
daM = New OleDbDataAdapter("SELECT * FROM Kim_Items_Result", con)
Try
con.Open()
daM.Fill(dtM)
con.Close()
dgvMain.DataSource = dtM
With dgvMain
.Columns("Item").HeaderText = "Part #"
.Columns("Item").Width = 150
.Columns("ItemDescription").Width = 600
.Columns("PrimaryVendorNumber").Width = 400
.Columns("PrimaryVendorNumber").HeaderText = "Vendor"
.ClearSelection()
End With
Catch ex As Exception
con.Close()
MsgBox(ex.Message)
End Try
Dim col, col2 As New DataGridViewTextBoxColumn
'col.Visible = False
dgvMain.Columns.Add(col)
dgvMain.Columns.Add(col2)
For i As Integer = 0 To dgvMain.RowCount - 1
If dgvMain.Rows(i).Cells("ItemDescription").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("ItemDescription").Style.BackColor = Color.LightGreen
End If
If dgvMain.Rows(i).Cells("Min").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Min").Style.BackColor = Color.GreenYellow
End If
If dgvMain.Rows(i).Cells("Max").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Max").Style.BackColor = Color.OrangeRed
End If
If dgvMain.Rows(i).Cells("PrimaryVendorNumber").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("PrimaryVendorNumber").Style.BackColor = Color.DarkOrange
End If
If dgvMain.Rows(i).Cells("StdUM").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("StdUM").Style.BackColor = Color.PeachPuff
End If
If dgvMain.Rows(i).Cells("UMConversion").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("UMConversion").Style.BackColor = Color.DarkSalmon
End If
Next
For i As Integer = 0 To dgvMain.RowCount - 1
If dgvMain.Rows(i).Cells(0).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(1).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(2).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(3).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(4).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(5).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(6).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
Next
dgvMain.ClearSelection()
lblTotal.Text = dgvMain.RowCount
End Sub
Vous devrez réassigner les couleurs après le tri (en-tête). Lorsque l'utilisateur clique sur l'en-tête, le mécanisme de tri remet les couleurs d'arrière-plan à leur état par défaut. Il peut être plus facile d'utiliser un mécanisme d'événement "cellule modifiée" pour formater une cellule modifiée à la couleur appropriée lorsque la condition est remplie. Sinon, vous pouvez toujours parcourir toutes les lignes et définir les couleurs lorsque la grille est triée ou qu'une valeur de cellule change. Choisis ton poison ;-). – JohnG
JohnG, vous êtes l'homme! merci beaucoup cela fonctionne parfaitement J'ai utilisé l'événement "Sorted" pour réaffecter la backcolor. – JCLD