DataGridView单元格数据的条件格式 - 更改负面

时间:2017-02-15 10:34:25

标签: vb.net datagridview colors formatting

我希望能够在DataCridView单元格的DefaultCellStyle.Format字段中使用基于颜色的条件格式,方式与Excel处理此方式的方式类似。

例如,在Excel中,格式字符串£#,## 0.00; [红色] - £#,## 0.00 将以红色显示负值。

这是否支持VB.NET?

我知道我可以使用.CellFormatting事件有条件地更改单元格文本颜色,但是正在寻找一种不那么笨重且限制性的方法。

2 个答案:

答案 0 :(得分:0)

Dim dgv As DataGridView = Me.DataGridView1 

    For i As Integer = 0 To dgv.Rows.Count - 1
        For ColNo As Integer = 4 To 7 ' number columns
            If Not dgv.Rows(i).Cells(ColNo).Value < 0 Then

                dgv.Rows(i).Cells(ColNo).Style.BackColor =  vbcolor.Red
            End If
        Next
    Next

检查负值以查找字符串格式并相应地检查

如果成功,Tryparse会将输入转换为整数 - 您不需要comps和value变量。以下是其工作原理的示例:

Dim comps As Integer
Dim input As String = "im not an integer"
Dim input2 As String = "2"

'tryparse fails, doesn't get into comps < 0 comparison
If Integer.TryParse(input, comps) Then
    If comps < 0 Then
        'do something
    End If
Else
   'I'm not an integer!
End If

'tryparse works, goes into comps < 0 comparison
If Integer.TryParse(input2, comps) Then
    If comps < 0 Then
        'do something
    End If
End If

答案 1 :(得分:0)

通过创建以下CellFormatting添加,我可以在单元格格式字段中使用Excel样式条件颜色格式。支持设置负/正/零值的颜色。

格式字符串应采用以下格式(所有颜色都可选):

[colour]<format for +value> ; [colour]<format for -value> ; [colour]<format for zero value>

..具有条件格式的测试DGV列

        c = New DataGridViewColumn
        c.Name = "AmountOUT"
        c.DataPropertyName = c.Name
        c.HeaderText = "AmountOUT"
        c.CellTemplate = New DataGridViewTextBoxCell
        c.DefaultCellStyle.Format = "[Green]£0.00;[Red]-£0.00;[Blue]zero"
        .Columns.Add(c)

...

    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    'Split format string to positive / negative / zero components
    Dim posnegzero As List(Of String)
    posnegzero = e.CellStyle.Format.Split(CChar(";")).ToList

    Dim coloursPNZ As New List(Of String)
    Dim remainderformatPNZ As String = ""

    For Each s As String In posnegzero
        If s.Contains("[") And s.Contains("]") Then
            'Extract [xxx] contents
            coloursPNZ.Add(s.Substring(s.IndexOf("[") + 1, s.IndexOf("]") - s.IndexOf("[") - 1))
            'Append rebuilt format excluding [xxx]
            remainderformatPNZ &= s.Substring(0, s.IndexOf("[")) & s.Substring(s.IndexOf("]") + 1, s.Length - s.IndexOf("]") - 1) & ";"
        Else
            coloursPNZ.Add("")
            remainderformatPNZ &= s & ";"
        End If
    Next

    'Set format excluding any [xxx] components
    e.CellStyle.Format = remainderformatPNZ

    'Check for positive value
    If Val(e.Value) > 0 And coloursPNZ.Count >= 1 Then
        If coloursPNZ(0) <> "" Then
            e.CellStyle.ForeColor = Color.FromName(coloursPNZ(0))
        End If
    End If

    'Check for negative value
    If Val(e.Value) < 0 And coloursPNZ.Count >= 2 Then
        If coloursPNZ(1) <> "" Then
            e.CellStyle.ForeColor = Color.FromName(coloursPNZ(1))
        End If
    End If

    'Check for zero value
    If Val(e.Value) = 0 And coloursPNZ.Count >= 3 Then
        If coloursPNZ(2) <> "" Then
            e.CellStyle.ForeColor = Color.FromName(coloursPNZ(2))
        End If
    End If
End Sub