DataGridView单元格数据的条件格式-更改颜色为负数 [英] Conditional formatting of DataGridView cell data - Change color on negative

查看:67
本文介绍了DataGridView单元格数据的条件格式-更改颜色为负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was hoping to be able to use color based conditional formatting in the DefaultCellStyle.Format field for DataGridView cells, in a similar way to how Excel handles this.

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

For example in Excel, a format string of £#,##0.00;[Red]-£#,##0.00 will display negative values in red.

VB.NET支持吗?

Is this supported in VB.NET ?

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

I am aware I can use the .CellFormatting event to conditionally change cell text color but was looking for a less bulky and restrictive way of doing this.

推荐答案

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

By creating the following CellFormatting addition, I am able to use Excel style conditional colour formatting in the cells format field. Setting the colour for negative/positive/zero values is supported.

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

Format string is expected to be in the following format (all colours optional) :

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

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

..a test DGV column with conditional formatting

        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

这篇关于DataGridView单元格数据的条件格式-更改颜色为负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆