マウスポインタがセル内に入ったとき、背景色を変更する
VB
Private Sub DataGridView1_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
If e.RowIndex < 0 Then Return
If e.ColumnIndex < 0 Then Return
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Yellow
End Sub
マウスポインタがセル内から出たとき、背景色を変更する
VB
Private Sub DataGridView1_CellMouseLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
If e.RowIndex < 0 Then Return
If e.ColumnIndex < 0 Then Return
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Empty
End Sub
動作確認用コード
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("Col1", GetType(String))
.Columns.Add("Col2", GetType(String))
.Rows.Add("A001", "B001")
.Rows.Add("A002", "B002")
End With
With DataGridView1
.DataSource = dt
End With
End Sub
コメント