给你一个示例
Private Sub DG1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DG1.CellFormatting
If DG1.Columns(e.ColumnIndex).Name = "Red1" Or DG1.Columns(e.ColumnIndex).Name = "Red2" Or DG1.Columns(e.ColumnIndex).Name = "Red3" Or DG1.Columns(e.ColumnIndex).Name = "Red4" Or DG1.Columns(e.ColumnIndex).Name = "Red5" Or DG1.Columns(e.ColumnIndex).Name = "Red6" Or DG1.Columns(e.ColumnIndex).Name = "Red7" Or DG1.Columns(e.ColumnIndex).Name = "Red8" Or DG1.Columns(e.ColumnIndex).Name = "Red9" Or DG1.Columns(e.ColumnIndex).Name = "Red10" Or DG1.Columns(e.ColumnIndex).Name = "Red11" Or DG1.Columns(e.ColumnIndex).Name = "Red12" Or DG1.Columns(e.ColumnIndex).Name = "Red13" Or DG1.Columns(e.ColumnIndex).Name = "Red14" Or DG1.Columns(e.ColumnIndex).Name = "Red15" Or DG1.Columns(e.ColumnIndex).Name = "Red16" Or DG1.Columns(e.ColumnIndex).Name = "Red17" Or DG1.Columns(e.ColumnIndex).Name = "Red18" Or DG1.Columns(e.ColumnIndex).Name = "Red19" Or DG1.Columns(e.ColumnIndex).Name = "Red20" Or DG1.Columns(e.ColumnIndex).Name = "Red21" Or DG1.Columns(e.ColumnIndex).Name = "Red22" Or DG1.Columns(e.ColumnIndex).Name = "Red23" Or DG1.Columns(e.ColumnIndex).Name = "Red24" Or DG1.Columns(e.ColumnIndex).Name = "Red25" Or DG1.Columns(e.ColumnIndex).Name = "Red26" Or DG1.Columns(e.ColumnIndex).Name = "Red27" Or DG1.Columns(e.ColumnIndex).Name = "Red28" Or DG1.Columns(e.ColumnIndex).Name = "Red30" Or DG1.Columns(e.ColumnIndex).Name = "Red31" Or DG1.Columns(e.ColumnIndex).Name = "Red32" Or DG1.Columns(e.ColumnIndex).Name = "Red33" Or DG1.Columns(e.ColumnIndex).Name = "Red29" Then
If Not (e.Value Is Nothing) Then
If Val(e.Value) > 0 Then
e.CellStyle.BackColor = Color.Red
End If
End If
ElseIf DG1.Columns(e.ColumnIndex).Name = "Blue1" Or DG1.Columns(e.ColumnIndex).Name = "Blue2" Or DG1.Columns(e.ColumnIndex).Name = "Blue3" Or DG1.Columns(e.ColumnIndex).Name = "Blue4" Or DG1.Columns(e.ColumnIndex).Name = "Blue5" Or DG1.Columns(e.ColumnIndex).Name = "Blue6" Or DG1.Columns(e.ColumnIndex).Name = "Blue7" Or DG1.Columns(e.ColumnIndex).Name = "Blue8" Or DG1.Columns(e.ColumnIndex).Name = "Blue9" Or DG1.Columns(e.ColumnIndex).Name = "Blue10" Or DG1.Columns(e.ColumnIndex).Name = "Blue11" Or DG1.Columns(e.ColumnIndex).Name = "Blue12" Or DG1.Columns(e.ColumnIndex).Name = "Blue13" Or DG1.Columns(e.ColumnIndex).Name = "Blue14" Or DG1.Columns(e.ColumnIndex).Name = "Blue15" Or DG1.Columns(e.ColumnIndex).Name = "Blue16" Then
If Not (e.Value Is Nothing) Then
If Val(e.Value) > 0 Then
e.CellStyle.BackColor = Color.Blue
End If
End If
End If
End Sub