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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
Public Class FormGantDGVSalle
Private monDataSet As New DataSet
Private relTableSalle As DataRelation = Nothing
Private ColorColums() As Color
Private Sub FormGantDGVSalle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.Dock = DockStyle.Top
Me.DataGridView2.Dock = DockStyle.Bottom 'ajout colonnes
'Fill la table "maTable"
GetData()
'Cree la table "maSalle"
CreateTableSalle()
'Cree la relation parent-child salle =>table
CreateRelationSalleTable()
'Ce 2eme DataGridView sert à afficher les donnees de la table maTable
'trie suivant colonne Salle
Dim view As DataView = monDataSet.Tables("maTable").DefaultView
view.Sort = "NumeroSalle ASC"
DataGridView2.DataSource = view
For I As Integer = 0 To 23
If I >= 0 And I <= 9 Then
Me.DataGridView1.Columns.Add("Heure" + I.ToString(), "0" & I & "h")
Else
Me.DataGridView1.Columns.Add("Heure" + I.ToString(), I & "h")
End If
Me.DataGridView1.Columns.Add("Minute10", "10")
Me.DataGridView1.Columns.Add("Minute20", "20")
Me.DataGridView1.Columns.Add("Minute30", "30")
Me.DataGridView1.Columns.Add("Minute40", "40")
Me.DataGridView1.Columns.Add("Minute50", "50")
Next I
'format colonnes
For I As Integer = 0 To Me.DataGridView1.ColumnCount - 1
If Me.DataGridView1.Columns(I).HeaderCell.Value Like "*h" Then
'TableGrapheDataGridView.Columns(I).HeaderCell.Style.BackColor = Color.LightGray
Me.DataGridView1.Columns(I).HeaderCell.Style.Font = New Font("Arial", 12, FontStyle.Bold)
End If
Next I
For Each _col As DataGridViewColumn In Me.DataGridView1.Columns
_col.SortMode = DataGridViewColumnSortMode.NotSortable
Next
'ajout lignes
Me.DataGridView1.TopLeftHeaderCell.Value = "Salle"
Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(0).MinimumHeight = 20
Me.DataGridView1.Rows(0).HeaderCell.Value = "Batiment 1"
For ZZ As Integer = 0 To Me.DataGridView1.Columns.Count - 1
Me.DataGridView1.Item(ZZ, 0).Style.BackColor = Color.LightGray
Next ZZ
'ajout des lignes de la table "maSalle" au DataGridView1
For Each item As DataRow In monDataSet.Tables("maSalle").Rows
Dim N As Integer = Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(N).MinimumHeight = 30
Me.DataGridView1.Rows(N).HeaderCell.Value = item(0).ToString
Next
'Fill array de couleurs
FillColorColums(DataGridView1)
End Sub
Private rnd As New Random
Private Sub GetData()
Dim maTable As New DataTable("maTable")
Dim colNom, colDateArrivee, colDateDepart, colSalle As DataColumn
colNom = New DataColumn("Nom", GetType(String))
colDateArrivee = New DataColumn("DateArrivee", GetType(DateTime))
colDateDepart = New DataColumn("DateDepart", GetType(DateTime))
colSalle = New DataColumn("NumeroSalle", GetType(Integer))
maTable.Columns.Add(colNom)
maTable.Columns.Add(colDateArrivee)
maTable.Columns.Add(colDateDepart)
maTable.Columns.Add(colSalle)
Dim XDateArrive As Date = Date.Now
Dim XDateDepart As Date
Dim heure, minute As Integer
Dim dr As DataRow = maTable.NewRow
For i As Integer = 0 To 19
dr(0) = "Nom" + (i + 1).ToString
heure = rnd.Next(6, 23)
minute = rnd.Next(30, 60)
XDateArrive = XDateArrive.AddHours(heure)
XDateArrive = XDateArrive.AddMinutes(minute)
heure = rnd.Next(6, 23)
minute = rnd.Next(30, 60)
XDateDepart = XDateArrive.AddHours(heure)
XDateDepart = XDateArrive.AddMinutes(minute)
If XDateDepart < XDateArrive Then
Dim temp As Date = XDateDepart
XDateDepart = XDateArrive
XDateArrive = temp
End If
dr(1) = XDateArrive
dr(2) = XDateDepart
dr(3) = rnd.Next(1, 10)
maTable.Rows.Add(dr)
dr = maTable.NewRow
Next
monDataSet.Tables.Add(maTable)
End Sub
Private Sub CreateTableSalle()
Dim view As DataView = monDataSet.Tables("maTable").DefaultView
view.Sort = "NumeroSalle ASC"
Dim maSalle As New DataTable("maSalle")
Dim colSalle As DataColumn
colSalle = New DataColumn("NumeroSalle", GetType(Integer))
maSalle.Columns.Add(colSalle)
Dim ligne As Integer = 0
Dim numero As Integer = view(ligne).Item(3)
Dim currentNumero As Integer = -1
Dim dr As DataRow = maSalle.NewRow
Do
If numero <> currentNumero Then
dr(0) = numero
maSalle.Rows.Add(dr)
dr = maSalle.NewRow
currentNumero = numero
End If
ligne += 1
numero = view(ligne).Item(3)
Loop Until ligne = view.Count - 1
monDataSet.Tables.Add(maSalle)
End Sub
Private Sub CreateRelationSalleTable()
Dim parentColumn As DataColumn = monDataSet.Tables("maSalle").Columns("NumeroSalle")
Dim childColumn As DataColumn = monDataSet.Tables("maTable").Columns("NumeroSalle")
' Create DataRelation.
relTableSalle =
New DataRelation("TableSalle", parentColumn, childColumn, False)
monDataSet.Relations.Add(relTableSalle)
End Sub
Private Sub FillColorColums(ByVal dgv As DataGridView)
ColorColums = New Color(dgv.Rows.Count) {}
Dim rndColor As New Random
For i As Integer = 0 To dgv.RowCount - 1
ColorColums(i) = Color.FromArgb(100, rndColor.Next(0, 256), rndColor.Next(0, 256), rndColor.Next(0, 256))
Next
End Sub
Private nomItem As String = String.Empty
Private myColor1 As Color = Color.Transparent
Private colDebut, colFin As DateTime
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If monDataSet.Tables("maSalle") Is Nothing Then Return
If (e.ColumnIndex >= 0 AndAlso e.RowIndex > 0) Then
Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1,
e.CellBounds.Width - 4, e.CellBounds.Height - 4)
Dim foreColorBrush As New SolidBrush(e.CellStyle.ForeColor)
Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor)
Dim gridLinePen As New Pen(gridBrush)
Dim diagramPen As New Pen(Brushes.Red, 2.0)
Try
' Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom)
'recupere la couleur Background de ColorColums
myColor1 = ColorColums(e.RowIndex)
'recupere le nom du datable
Dim salleRow As DataRow = monDataSet.Tables("maSalle").Rows(e.RowIndex - 1)
For Each row As DataRow In salleRow.GetChildRows(relTableSalle)
nomItem = row.Item(0).ToString()
'recupere colonne debut apres conversion des heures et minutes
'en abscisse colonne
colDebut = row.Item(1)
Dim x1 As Integer = colDebut.Hour * 6
x1 += colDebut.Minute / 10
'recupere colonne fin (idem)
colFin = row.Item(2)
Dim x2 As Integer = colFin.Hour * 6
x2 += colFin.Minute / 10
If e.ColumnIndex >= x1 And e.ColumnIndex <= x2 Then ' dans la plage ?
e.CellStyle.BackColor = myColor1
backColorBrush.Color = myColor1
' Fill the background cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
' Draw the text content of the cell (1 seule fois)
If e.ColumnIndex = x1 + 1 Then
e.Graphics.DrawString(nomItem, e.CellStyle.Font,
foreColorBrush, e.CellBounds.X + 2, e.CellBounds.Y + 2,
StringFormat.GenericDefault)
End If
' Draw the bar diagram
Dim p0 As New Point
Dim p1 As New Point
p0.X = e.CellBounds.Left
p0.Y = e.CellBounds.Top + e.CellBounds.Height / 2
p1.X = e.CellBounds.Right
p1.Y = p0.Y
e.Graphics.DrawLine(diagramPen, p0, p1)
End If
Next
e.Handled = True
Finally
gridLinePen.Dispose()
gridBrush.Dispose()
foreColorBrush.Dispose()
backColorBrush.Dispose()
diagramPen.Dispose()
End Try
End If
End Sub
End Class |
Partager