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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
Public Class Form3
'gestion des colonnes speciales : DataGridViewLinkColumn et DataGridViewButtonColum
Enum ColumnName
LinkColumn
ButtonColumn
End Enum
Private yourTable As DataTable
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
yourTable = LoadTable()
Me.DataGridView1.DataSource = yourTable
'suppression des colonnes autogenererees
Me.DataGridView1.Columns.Remove(ColumnName.ButtonColumn.ToString())
Me.DataGridView1.Columns.Remove(ColumnName.LinkColumn.ToString())
'leur remplacement par les colonnes appropriées
AddButtonColumn()
AddLinkColumn()
End Sub
'datatable exemple
Private Function LoadTable() As DataTable
Dim tbl As New DataTable
tbl.Columns.AddRange(
New DataColumn() {
New DataColumn("Numero", GetType(Integer)),
New DataColumn("Text", GetType(String)),
New DataColumn("LinkColumn", GetType(String)),
New DataColumn("Image", GetType(Image)),
New DataColumn("ButtonColumn", GetType(String))
})
tbl.Rows.Add(New Object() {100, "item1", "https://www.developpez.net/forums/d2098368/dotnet/langages/vb-net/placer-tableau-donnees-1-cellule-datagridview/", ImageList1.Images(0), "valider"})
tbl.Rows.Add(New Object() {100, "item1", "https://www.google.fr", ImageList1.Images(0), "valider"})
tbl.Rows.Add(New Object() {200, "item2", "https://www.yahoo.fr", ImageList1.Images(1), "valider"})
tbl.Rows.Add(New Object() {300, "item3", "https://www.google.fr", ImageList1.Images(2), "valider"})
tbl.Rows.Add(New Object() {300, "item3", "https://www.developpez.net/forums/f486/dotnet/langages/vb-net/", ImageList1.Images(2), "valider"})
tbl.Rows.Add(New Object() {300, "item3", "https://www.developpez.net/forums/d2098368/dotnet/langages/vb-net/placer-tableau-donnees-1-cellule-datagridview/", ImageList1.Images(2), "valider"})
Return tbl
End Function
Private Sub AddLinkColumn()
Dim lnk As New DataGridViewLinkColumn()
With lnk
.Name = ColumnName.LinkColumn.ToString()
.HeaderText = ColumnName.LinkColumn.ToString()
.DataPropertyName = ColumnName.LinkColumn.ToString()
.ActiveLinkColor = Color.White
.LinkBehavior = LinkBehavior.SystemDefault
.LinkColor = Color.Blue
.TrackVisitedState = True
.VisitedLinkColor = Color.YellowGreen
End With
Me.DataGridView1.Columns.Add(lnk)
End Sub
Private Sub AddButtonColumn()
Dim btn As New DataGridViewButtonColumn()
With btn
.Name = ColumnName.ButtonColumn.ToString()
.HeaderText = ColumnName.ButtonColumn.ToString()
.DataPropertyName = ColumnName.ButtonColumn.ToString()
End With
Me.DataGridView1.Columns.Add(btn)
End Sub
'les events CellFormatting & CellPainting
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal args As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
' First row always displays
If (args.RowIndex = 0) Then Return
If (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex)) Then
args.Value = String.Empty
args.FormattingApplied = True
End If
End Sub
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal args As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
'Ignore column and row headers and first row
If (args.RowIndex < 1 Or args.ColumnIndex < 0) Then Return
If (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex)) Then
args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
Else
args.AdvancedBorderStyle.Top = Me.DataGridView1.AdvancedCellBorderStyle.Top
args.AdvancedBorderStyle.Right = Me.DataGridView1.AdvancedCellBorderStyle.Right
args.AdvancedBorderStyle.Bottom = Me.DataGridView1.AdvancedCellBorderStyle.Bottom
End If
End Sub
'methode au coeur de ce custom formatting :grouping des columns
Private Function IsRepeatedCellValue(ByVal RowIndex As Integer, ByVal colIndex As Integer) As Boolean
If RowIndex = Me.DataGridView1.RowCount - 1 Then Return False 'saut nouvelle ligne
Dim currCell As DataGridViewCell = Me.DataGridView1.Rows(RowIndex).Cells(colIndex)
Dim prevCell As DataGridViewCell = Me.DataGridView1.Rows(RowIndex - 1).Cells(colIndex)
'gestion cellule Bitmap
If (TypeOf currCell.Value Is Bitmap) Then Return False 'gestion
'gestion de cellule nouvelle ligne
If (TypeOf currCell.Value Is DBNull) Then Return False
'gestion de la cellule de type DGVBoutonCell(une telle cellule comporte un contenu texte fixe et repetitif)
If Me.DataGridView1.Columns(colIndex).Name = ColumnName.ButtonColumn.ToString() Then
Return False
End If
'gestion des cellules de type DGVTextBoxCell (une telle cellule comporte un contenu texte fixe et repetitif)
If (currCell.Value = prevCell.Value) Or
(currCell.Value IsNot Nothing And prevCell.Value IsNot Nothing And
currCell.Value.ToString() = prevCell.Value.ToString()) Then
Return True
Else
Return False
End If
End Function
End Class |
Partager