Bonjour,
J'aimerais savoir s'il existe un moyen de modifier la couleur de fond d'une ligne d'un datagrid en fonction d'une valeur dans une datarow ...
Au pire, la police ...
Merci d'avance !
Bonjour,
J'aimerais savoir s'il existe un moyen de modifier la couleur de fond d'une ligne d'un datagrid en fonction d'une valeur dans une datarow ...
Au pire, la police ...
Merci d'avance !
Bon ben ta de la chance parceque moi j'ai mis des jours a trouver ca!!!
Créer la classe « DataGridFormatCellEventArgs » :
Puis ensuite créer cette procédure dans la code de votre Form Principale. C’est elle qui va créer les « ColumnStyles » nécessaires.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 Option Strict OffOption Explicit On Imports Microsoft.VisualBasicImports SystemImports System.DrawingPublic Class DataGridFormatCellEventArgs Inherits EventArgs Private _column As Integer Private _row As Integer Private _font As Font Private _backBrush As Brush Private _foreBrush As Brush Private _useBaseClassDrawing As Boolean Public Sub New(ByVal row As Integer, ByVal col As Integer, ByVal font1 As Font, ByVal backBrush As Brush, ByVal foreBrush As Brush) MyBase.New() _row = row _column = col _font = font1 _backBrush = backBrush _foreBrush = foreBrush _useBaseClassDrawing = False End Sub Public Property Column() As Integer Get Return _column End Get Set(ByVal Value As Integer) _column = Value End Set End Property Public Property Row() As Integer Get Return _row End Get Set(ByVal Value As Integer) _row = Value End Set End Property Public Property TextFont() As Font Get Return _font End Get Set(ByVal Value As Font) _font = Value End Set End Property Public Property BackBrush() As Brush Get Return _backBrush End Get Set(ByVal Value As Brush) _backBrush = Value End Set End Property Public Property ForeBrush() As Brush Get Return _foreBrush End Get Set(ByVal Value As Brush) _foreBrush = Value End Set End Property Public Property UseBaseClassDrawing() As Boolean Get Return _useBaseClassDrawing End Get Set(ByVal Value As Boolean) _useBaseClassDrawing = Value End Set End Property End Class
Il faut ensuite créer une nouvelle classe « DataGridFormattableTextBoxColumn » qui va surcharger la méthode « Paint » :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 Public Sub AddCellFormattingColumnStyles(ByVal DataGridCible As DataGrid, ByVal handler As FormatCellEventHandler) Dim monTableStyle As DataGridTableStyle Dim maDataTable As DataTable Dim j As Integer j = 0 monTableStyle = New DataGridTableStyle maDataTable = CType(DataGridCible.DataSource, DataTable) monTableStyle.MappingName = maDataTable.TableName Do While (j < maDataTable.Columns.Count) Dim maColonne As DataGridFormattableTextBoxColumn maColonne = New DataGridFormattableTextBoxColumn(j) maColonne.MappingName = maDataTable.Columns(j).ColumnName maColonne.HeaderText = maDataTable.Columns(j).ColumnName AddHandler maColonne.SetCellFormat, handler monTableStyle.GridColumnStyles.Add(maColonne) j = (j + 1) Loop DataGridCible.TableStyles.Clear() DataGridCible.TableStyles.Add(monTableStyle) End Sub
Il faut aussi créer une classe « FormatCellEventHandler» :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 Option Strict OffOption Explicit On Imports Microsoft.VisualBasicImports SystemImports System.DrawingImports System.Windows.FormsPublic Class DataGridFormattableTextBoxColumn Inherits DataGridTextBoxColumn Private _col As Integer Public Sub New(ByVal col As Integer) MyBase.New() _col = col End Sub Public Event SetCellFormat As FormatCellEventHandler Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean) Dim e As DataGridFormatCellEventArgs e = New DataGridFormatCellEventArgs(rowNum, Me._col, Me.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush) RaiseEvent SetCellFormat(Me, e) If e.UseBaseClassDrawing Then MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight) Else g.FillRectangle(e.BackBrush, bounds) g.DrawString(Me.GetColumnValueAtRow(source, rowNum).ToString, e.TextFont, e.ForeBrush, bounds.X, bounds.Y) End If If (e.TextFont Is Me.DataGridTableStyle.DataGrid.Font) = False Then e.TextFont.Dispose() End If End Sub Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean) 'Pour rendre les cellules non-éditable MyBase.Edit(source, rowNum, bounds, [readOnly], instantText, cellIsVisible) End SubEnd Class
Il ne reste plus qu’à créer un TableStyle dans le code de votre Form Principale :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 Option Strict OffOption Explicit On Imports Microsoft.VisualBasicImports SystemPublic Delegate Sub FormatCellEventHandler(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs) Il ne reste plus quà créer la fonction « FormatGridCellsNomDuDataGrid » dans le code de la Form Principale: Public Sub FormatGridCellsNomDuDataGrid(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs) Dim maDataTable As DataTable Dim monObjet As Object Dim Chaine As String maDataTable = CType(NomDuDataGrid.DataSource, DataTable) monObjet = NomDuDataGrid (e.Row, _NumCol) Chaine = StrConv(monObjet, VbStrConv.UpperCase) If Chaine Like "SELEC" Then e.BackBrush = Brushes.DodgerBlue End IfEnd Sub
Voila bon courage j'ai pas le temps de t'expliquer plus, j'espère que ca suffira
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 'Création des styles des tables et des colonnes pour Overides Paint(couleur cellules) monTableStyle.MappingName = "Table"NomDuDataGrid.TableStyles.Add(monTableStyle)NomDuDataGrid.DataSource = monDataSet.Tables(0) 'Pour mettre en couleur les lignes de la table AddCellFormattingColumnStyles(NomDuDataGrid, New FormatCellEventHandler (AddressOf FormatGridCellsNomDuDataGrid))
Merci
Dans le même temps, j'ai trouvé cela !
la même chose avec l'explication
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q745q
C'est la que je l'avais trouvé mais j'avais un petit bout de temps avant d'arriver a l'adapter (je suis encore novice en VB.Net)
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager