■■■

2016年4月1日金曜日

DataGridViewのセルフォントを変更する方法

DataGridViewのセルフォントを変更する方法
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.Font = New Font(dgv1.Font, _
dgv1.Font.Style Or FontStyle.Bold)
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.Font = new Font(dgv1.Font,
dgv1.Font.Style | FontStyle.Bold);
}

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;
}
}


















■■■