Private Sub AddDGV(dt As DataTable, tp As TabPage)
Dim dgv As New DataGridView
AddHandler dgv.RowPrePaint, AddressOf dgvRowPrePaint
With dgv
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.AutoGenerateColumns = False
.ScrollBars = ScrollBars.Both
.Dock = DockStyle.Fill
.RowHeadersVisible = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ColumnHeadersVisible = False
.ColumnCount = 2
.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
.Columns(0).Width = 40
.Columns(0).CellTemplate.Style.Font = New Drawing.Font("Segoe UI", 7, FontStyle.Bold)
.Columns(0).DataPropertyName = dt.Columns(4).ColumnName
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Columns(1).CellTemplate.Style.Font = New Drawing.Font("Segoe UI", 6)
.RowTemplate.Height = 40
.Location = Point.Empty
.Parent = tp
.DataSource = dt
End With
End Sub
Private Sub dgvRowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs)
Static cell0Format As New StringFormat(StringFormat.GenericTypographic) With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center, .FormatFlags = StringFormat.GenericTypographic.FormatFlags Or StringFormatFlags.NoWrap}
If e.RowIndex > -1 Then
Dim dgv As DataGridView = DirectCast(sender, DataGridView)
Dim dt As DataTable = DirectCast(dgv.DataSource, DataTable)
Dim row As DataRow = dt.Rows(e.RowIndex)
Dim Column0 As DataGridViewColumn = dgv.Columns(0)
Dim cell0Bounds As Rectangle = dgv.GetCellDisplayRectangle(0, e.RowIndex, False)
Dim cell1Bounds As Rectangle = dgv.GetCellDisplayRectangle(1, e.RowIndex, False)
e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceOver
If ContainsAndRemovePart(e.PaintParts, DataGridViewPaintParts.Background) Then
Dim cell0Backcolor As Color = ColorTranslator.FromHtml(row.Item(3).ToString)
Using br As New SolidBrush(cell0Backcolor)
e.Graphics.FillRectangle(br, cell0Bounds)
End Using
Using br As New SolidBrush(System.Drawing.Color.FromArgb(83, 83, 83))
e.Graphics.FillRectangle(br, cell1Bounds)
End Using
End If
If ContainsAndRemovePart(e.PaintParts, DataGridViewPaintParts.ContentForeground) Then
Dim cell0Text As String = CStr(row.Item(4))
Dim textSize As SizeF = e.Graphics.MeasureString(cell0Text, Column0.CellTemplate.Style.Font, Column0.Width, cell0Format)
Dim textRect As New RectangleF(PointF.Empty, textSize)
textRect.Inflate(2, 2)
textRect.X = ((cell0Bounds.Width / 2.0F) - (textRect.Width / 2.0F)) + cell0Bounds.X
textRect.Y = ((cell0Bounds.Height / 2.0F) - (textRect.Height / 2.0F)) + cell0Bounds.Y
Using br As New SolidBrush(System.Drawing.Color.FromArgb(83, 83, 83))
e.Graphics.FillRectangle(br, textRect)
End Using
Dim fnt As Font = Column0.CellTemplate.Style.Font
e.Graphics.DrawString(cell0Text, fnt, Brushes.White, textRect, cell0Format)
Dim cell1Line1 As String = CStr(row.Item(1))
Dim linePt As Point = cell1Bounds.Location
linePt.Offset(5, 5)
e.Graphics.DrawString(cell1Line1, fnt, Brushes.White, linePt)
Dim cell1Line2 As String = CStr(row.Item(2))
fnt = dgv.Columns(1).CellTemplate.Style.Font
linePt.Offset(0, fnt.Height + 5)
e.Graphics.DrawString(cell1Line1, fnt, Brushes.White, linePt)
End If
End If
End Sub
Private Function ContainsAndRemovePart(ByRef parts As DataGridViewPaintParts, checkPart As DataGridViewPaintParts) As Boolean
Dim ret As Boolean = (parts And checkPart) = checkPart
If ret Then parts = parts And Not checkPart
Return ret
End Function