簡體   English   中英

DataGridView單元格數據的條件格式 - 在負面更改顏色

[英]Conditional formatting of DataGridView cell data - Change color on negative

我希望能夠在DataGridView單元格的DefaultCellStyle.Format字段中使用基於顏色的條件格式,方式與Excel處理此方法的方式類似。

例如,在Excel中,格式字符串為£#,## 0.00; [紅色] - £#,## 0.00將以紅色顯示負值。

這是否支持VB.NET?

我知道我可以使用.CellFormatting事件有條件地更改單元格文本顏色,但是正在尋找一種不那么笨重和限制性的方法。

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

通過創建以下CellFormatting添加,我可以在單元格格式字段中使用Excel樣式條件顏色格式。 支持設置負/正/零值的顏色。

格式字符串應采用以下格式(所有顏色可選):

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

..a測試具有條件格式的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM