2017-02-15 129 views
1

我希望能夠在DataGridView單元格的DefaultCellStyle.Format字段中使用基於顏色的條件格式,這與Excel處理此操作的方式類似。DataGridView單元格數據的條件格式 - 更改負面上的顏色

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

VB.NET支持嗎?

我知道我可以使用.CellFormatting事件來有條件地更改單元格文本的顏色,但是正在尋找一個不那麼笨重和限制性的方式來做到這一點。

+0

簡短回答是不,WinForms DataGridView沒有這樣的功能,不應該被認爲是電子表格組件。如果用戶可以更改單元格值,則需要查看「CellFormatting」或「RowsAdded」事件,也許需要查看「CellEndEdit」或「CommitEdit」。根據您的數據來源,可能會有更有效的事件發生。 – MrGadget

回答

0

通過創建以下CellFormatting另外,我可以使用Excel風格有條件的顏色格式的單元格格領域。支持設置負值/正值/零值的顏色。

格式字符串預計將在下面的格式(所有顏色可選):使用條件格式

 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) 

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

..A測試DGV柱..

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 
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將輸入轉換爲整數,如果它成功了 - 你不需要同時譜曲和值的變量。下面是它如何工作的例子:

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