タイトル : Re: Datagridview の BackColor について 投稿日 : 2008/01/23(Wed) 22:18 投稿者 : 魔界の仮面弁士
Public Class Form1 Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _ Handles MyBase.Load Me.DataGridView1.DataSource = CreateDummyData() End Sub 'チェックボックスの状態によって、セルの色が変化! Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) _ Handles CheckBox1.CheckedChanged Me.DataGridView1.Invalidate() '再描画を指示 End Sub '★これが本題★ Sub DataGridView1_CellFormatting(ByVal sender As Object, _ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles DataGridView1.CellFormatting If Not CheckBox1.Checked Then Return Dim c As Color = Me.DataGridView1.DefaultCellStyle.BackColor Dim i As Integer If Integer.TryParse(e.Value.ToString(), i) Then If i >= 5000 Then c = Color.Cyan ElseIf i >= 1000 Then c = Color.Red End If End If e.CellStyle.BackColor = c End Sub Function CreateDummyData() As DataTable CreateDummyData = New DataTable() With CreateDummyData .Columns.Add("F1", GetType(Integer)) .Columns.Add("F2", GetType(Integer)) Dim r As New Random() For n As Integer = 1 To 2000 .Rows.Add(n, r.Next(3000, 10000)) Next End With End Function End Class |