■■■

2016年4月1日金曜日

DataGridViewのセル背景に画像を描画する方法

DataGridViewのセル背景に画像を描画する方法
VB.NET
Private cellBackImage As New Bitmap("C:\img001.gif")

Private Sub dgv1_CellPainting(ByVal sender As Object, _
ByVal e As DataGridViewCellPaintingEventArgs) _
Handles dgv1.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 AndAlso _
(e.PaintParts And DataGridViewPaintParts.Background) = _
DataGridViewPaintParts.Background Then
Dim backParts As DataGridViewPaintParts = _
e.PaintParts And (DataGridViewPaintParts.Background Or _
DataGridViewPaintParts.SelectionBackground)
e.Paint(e.ClipBounds, backParts)

e.Graphics.DrawImage(cellBackImage, e.CellBounds)

Dim paintParts As DataGridViewPaintParts = _
e.PaintParts And Not backParts
e.Paint(e.ClipBounds, paintParts)

e.Handled = True
End If
End Sub
C#
private Bitmap cellBackImage = new Bitmap("C:\\img001.gif");

private void dgv1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&
(e.PaintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
DataGridViewPaintParts backParts = e.PaintParts &
(DataGridViewPaintParts.Background |
DataGridViewPaintParts.SelectionBackground);
e.Paint(e.ClipBounds, backParts);

e.Graphics.DrawImage(cellBackImage, e.CellBounds);

DataGridViewPaintParts paintParts =
e.PaintParts & ~backParts;
e.Paint(e.ClipBounds, paintParts);

e.Handled = true;
}
}




















■■■