画面イメージ
いずれかのスタイルが変更されたとき、ログを出力する
VB
Private Sub DataGridView1_CellStyleContentChanged(sender As Object, e As DataGridViewCellStyleContentChangedEventArgs) Handles DataGridView1.CellStyleContentChanged
Debug.WriteLine($"Style:{e.CellStyle.BackColor }")
End Sub
イベント発火のため、スタイルを変更する
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DataGridView1(0, 0).Style.BackColor = Color.Red
DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.Yellow
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Gray
DataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Gold
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Pink
DataGridView1.RowsDefaultCellStyle.BackColor = Color.Aqua
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Purple
DataGridView1.DefaultCellStyle.BackColor = Color.Orange
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("ProductCode", GetType(String))
.Columns.Add("Date", GetType(String))
.Rows.Add("A001", "2023/03/12")
.Rows.Add("A002", "2000/02/04")
.Rows.Add("A003", "2020/12/10")
.Rows.Add("A004", "2026/09/01")
End With
With DataGridView1
.DataSource = dt
End With
End Sub
コメント