16,554
社区成员
发帖
与我相关
我的任务
分享
'CellFormatting事件处理器
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
Dim dgv As DataGridView = CType(sender, DataGridView)
'确认单元格的列
If dgv.Columns(e.ColumnIndex).Name = "Column1" AndAlso _
TypeOf e.Value Is Integer Then
Dim val As Integer = CInt(e.Value)
'根据单元格的值,变更背景色
If val < 0 Then
e.CellStyle.BackColor = Color.Red
Else
e.CellStyle.BackColor = Color.Blue
End If
End If
End Sub