■■■

2016年4月1日金曜日

DataGridViewセルの選択時の枠を非表示にする方法

DataGridViewセルの選択時の枠を非表示にする方法
VB.NET
Private Sub dgv1_CellPainting(ByVal sender As Object, _
ByVal e As DataGridViewCellPaintingEventArgs) _
Handles dgv1.CellPainting
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim paintParts As DataGridViewPaintParts = _
e.PaintParts And Not DataGridViewPaintParts.Focus
e.Paint(e.ClipBounds, paintParts)
e.Handled = True
End If
End Sub
C#
private void dgv1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridViewPaintParts paintParts =
e.PaintParts & ~DataGridViewPaintParts.Focus;
e.Paint(e.ClipBounds, paintParts);
e.Handled = true;
}
}
RowPrePaintイベントを活用して選択枠を非表示化する方法
VB.NET
Private Sub dgv1_RowPrePaint(ByVal sender As Object, _
ByVal e As DataGridViewRowPrePaintEventArgs) _
Handles dgv1.RowPrePaint
e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus
End Sub
C#
private void dgv1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts &= ~DataGridViewPaintParts.Focus;
}

■■■