■■■

2016年4月1日金曜日

DataGridViewで特定のセルの色を変更する方法

DataGridViewで特定のセルの色を変更する方法
VB.NET
Private Sub dgv1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dgv1.CellMouseEnter
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim
dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Red
End If
End Sub


Private Sub dgv1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dgv1.CellMouseLeave
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim
dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Empty
dgv(e.ColumnIndex, e.RowIndex).Style.SelectionBackColor = Color.Empty
End If
End Sub
C#
private void dgv1_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red;
}
}

private void dgv1_CellMouseLeave(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Empty;
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Empty;
}
}
DataGridViewCellStyleを複数セルに適用する方法
VB.NET
Private defaultCellStyle As DataGridViewCellStyle
Private mouseCellStyle As DataGridViewCellStyle

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.defaultCellStyle = New DataGridViewCellStyle()
Me.mouseCellStyle = New DataGridViewCellStyle()
Me.mouseCellStyle.BackColor = Color.Red
Me.mouseCellStyle.SelectionBackColor = Color.Red
End Sub

Private Sub dgv1_CellMouseEnter(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dgv1.CellMouseEnter
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim
dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style = Me.mouseCellStyle
End If
End Sub


Private Sub dgv1_CellMouseLeave(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles dgv1.CellMouseLeave
If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
Dim
dgv As DataGridView = CType(sender, DataGridView)
dgv(e.ColumnIndex, e.RowIndex).Style = Me.defaultCellStyle
End If
End Sub
C#
private DataGridViewCellStyle defaultCellStyle;
private DataGridViewCellStyle mouseCellStyle;

private void Form1_Load(object sender, EventArgs e)
{
this.defaultCellStyle = new DataGridViewCellStyle();
this.mouseCellStyle = new DataGridViewCellStyle();
this.mouseCellStyle.BackColor = Color.Red;
this.mouseCellStyle.SelectionBackColor = Color.Red;
}

private void dgv1_CellEnter(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style = this.mouseCellStyle;
}
}

private void dgv1_CellLeave(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
DataGridView dgv = (DataGridView)sender;
dgv[e.ColumnIndex, e.RowIndex].Style = this.defaultCellStyle;
}
}

ヘッダーのセル色を変更する方法
VB.NET
dgv1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow
dgv1.RowHeadersDefaultCellStyle.BackColor = Color.Red
dgv1.TopLeftHeaderCell.Style.BackColor = Color.Blue
C#
dgv1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow;
dgv1.RowHeadersDefaultCellStyle.BackColor = Color.Red;
dgv1.TopLeftHeaderCell.Style.BackColor = Color.Blue;




















■■■