同一DataGridView单元格中的不同字体颜色

时间:2012-11-01 16:49:39

标签: vb.net datagridview

有没有办法在DataGridView的同一个单元格中使用2种不同的颜色?我正在使用vb.net 2010。

1 个答案:

答案 0 :(得分:0)

将代码放入DataGridView.CellFormatting事件

Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _
    ByVal e As DataGridViewCellFormattingEventArgs) _
    Handles dataGridView1.CellFormatting
    ' If the column is the Artist column, check the 
    ' value. 
    If Me.dataGridView1.Columns(e.ColumnIndex).Name _
        = "Artist" Then 
        If e.Value IsNot Nothing Then 

            ' Check for the string "pink" in the cell. 
            Dim stringValue As String = _
            CType(e.Value, String)
            stringValue = stringValue.ToLower()
            If ((stringValue.IndexOf("pink") > -1)) Then
                e.CellStyle.BackColor = Color.Pink
            End If 

        End If 
    ElseIf Me.dataGridView1.Columns(e.ColumnIndex).Name _
        = "Release Date" Then
        ShortFormDateFormat(e)
    End If 
End Sub

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx