Hallo!
Falls Du eine Date-Spalte hast und in einer weiteren Spalte ("Icon")
daten-bezogen eine Bitmap eintragen willst, kannst Du das CellPainting-Event
des Grid dafür nutzen.
Windows.Forms-Beispiel (untypisierte Datenbindung einer Datatable):
Public Class frmDGVDateIcon
Dim bs As New BindingSource
Dim WithEvents dgv As New DataGridView With
{.Parent = Me, .Size = New Drawing.Size(300, 300), .DataSource = bs}
Dim dt As New DataTable
Dim dateok, nodate As Bitmap
Private Sub frmDGVDateIcon_Load(sender As Object,
e As EventArgs) Handles MyBase.Load
'Einige Demo-Daten
dt.Columns.Add("Date", GetType(DateTime))
dt.Columns.Add("Icon", GetType(Bitmap))
With dt.Rows
.Add(Nothing, Nothing)
.Add(New DateTime(2020, 12, 10), Nothing)
.Add(New DateTime(2020, 12, 13), Nothing)
.Add(Nothing, Nothing)
.Add(New DateTime(2020, 12, 16), Nothing)
End With
'Datenbindung herstellen
bs.DataSource = dt
dgv.Columns("Icon").MinimumWidth = 16
'Zwei Bitmaps für die Icon-Spalte erstellen
nodate = New Bitmap(16, 16)
Using g As Graphics = Graphics.FromImage(nodate),
pn As New Pen(Color.White)
g.Clear(Color.Red)
g.DrawLine(pn, New Point(0, 0), New Point(16, 16))
g.DrawLine(pn, New Point(16, 0), New Point(0, 16))
End Using
dateok = New Bitmap(16, 16)
Using g As Graphics = Graphics.FromImage(dateok),
pn As New Pen(Color.Black)
g.Clear(Color.Green)
g.DrawLine(pn, New Point(0, 0), New Point(16, 16))
End Using
End Sub
Private Sub dgv_CellPainting(sender As Object,
e As DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
If (e.RowIndex >= 0 And e.RowIndex < bs.Count)
AndAlso e.ColumnIndex = 1 Then
'Gebundene Datatable-Zeile casten
Dim r As DataRow = DirectCast(bs(e.RowIndex), DataRowView).Row
'Icon-Zelle zeichnen
e.PaintBackground(e.CellBounds, True)
'Icon in der Zelle zentrieren
Dim lft As Integer =
e.CellBounds.Left + (e.CellBounds.Width - 16) \ 2
Dim tp As Integer =
e.CellBounds.Top + (e.CellBounds.Height - 16) \ 2
'Icon-Spalte abhängig vom Eintrag in der Date-Spalte zeichnen
If IsDBNull(r("Date")) Then
e.Graphics.DrawImage(nodate, lft, tp)
Else
e.Graphics.DrawImage(dateok, lft, tp)
End If
'Icon-Zelle als bearbeitet melden
e.Handled = True
End If
End Sub
End Class |