Imports System.Drawing.Drawing2D Imports System.ComponentModel Public Class Form1 Private WithEvents dgv As DataGridView Private tbl As DataTable Private Const MaxColumn As Integer = 50 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 'DataGridView の生成コード '通常はデザイン時に設定しておけば OK InitializeComponent2() '表示させるためのデータを用意 tbl = CreateTable() dgv.DataSource = tbl 'タイムチャートの編集は この DataTable に対して行う FillSample(tbl, 30) End Sub Private Function CreateTable() As DataTable Dim ds As New DataSet() Dim tbl = ds.Tables.Add("BitTable") tbl.PrimaryKey = New DataColumn() {tbl.Columns.Add("Address", GetType(String))} For c = 1 To MaxColumn tbl.Columns.Add(CStr(c), GetType(Boolean)).AllowDBNull = False Next Return tbl End Function Private Sub FillSample(ByVal tbl As DataTable, recordCount As Integer) Dim rand As New Random() For adrNum = 1 To recordCount Dim newRow = tbl.NewRow() newRow("Address") = "X" & CStr(adrNum) For Each col In tbl.Columns.OfType(Of DataColumn).Skip(1) newRow(col) = CBool(rand.Next() And 2) Next tbl.Rows.Add(newRow) Next tbl.AcceptChanges() End Sub Private Sub InitializeComponent2() dgv = New DataGridView() Dim design As ISupportInitialize = dgv design.BeginInit() dgv.Dock = DockStyle.Fill dgv.ReadOnly = True dgv.RowHeadersVisible = False dgv.ColumnHeadersVisible = True dgv.ColumnHeadersHeightSizeMode = _ DataGridViewColumnHeadersHeightSizeMode.DisableResizing dgv.AllowUserToAddRows = False dgv.AllowUserToDeleteRows = False dgv.AllowUserToOrderColumns = False dgv.AllowUserToResizeRows = False dgv.ShowCellToolTips = False dgv.ScrollBars = ScrollBars.Both Dim col As DataGridViewTextBoxColumn col = New DataGridViewTextBoxColumn() col.Name = "colAddress" col.DataPropertyName = "Address" col.HeaderText = "アドレス" col.Frozen = True col.DividerWidth = 2 col.SortMode = DataGridViewColumnSortMode.Programmatic col.Resizable = True col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells dgv.Columns.Add(col) For c As Integer = 1 To MaxColumn col = New DataGridViewTextBoxColumn() col.Name = "col" & CStr(c) col.DataPropertyName = CStr(c) col.HeaderText = CStr(c) col.Width = 24 col.SortMode = DataGridViewColumnSortMode.Programmatic col.Resizable = DataGridViewTriState.False col.AutoSizeMode = DataGridViewAutoSizeColumnMode.None dgv.Columns.Add(col) Next Controls.Add(dgv) design.EndInit() End Sub Private Sub dgv_CellPainting(sender As Object, _ e As DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting If e.RowIndex < 0 OrElse e.ColumnIndex <= 0 Then Return End If If e.ColumnIndex Mod 5 <> 0 Then e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None End If 'タイムチャート セルの On/Off で色を変える Dim brs As Brush If CBool(e.Value) Then brs = New LinearGradientBrush(e.CellBounds, Color.SpringGreen, _ Color.LightSeaGreen, LinearGradientMode.ForwardDiagonal) Else brs = New SolidBrush(SystemColors.Window) End If e.Graphics.FillRectangle(brs, e.CellBounds) e.Paint(e.CellBounds, DataGridViewPaintParts.Border) brs.Dispose() If dgv.CurrentCellAddress = New Point(e.ColumnIndex, e.RowIndex) Then ControlPaint.DrawFocusRectangle(e.Graphics, e.CellBounds) End If e.Handled = True End Sub Private Sub dgv_CellDoubleClick(sender As Object, _ e As DataGridViewCellEventArgs) Handles dgv.CellDoubleClick If e.RowIndex > 0 AndAlso e.ColumnIndex > 0 Then Dim cell As DataGridViewCell = dgv(e.ColumnIndex, e.RowIndex) If cell.ValueType Is GetType(Boolean) Then 'ダブルクリックで On/Off 切り替え cell.Value = Not CBool(cell.Value) End If End If End Sub End Class