Click here to Skip to main content
11,414,561 members (69,398 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
- I want to change the text color of the cell when meet the condition I set.
- I also want to draw the horizontal line in every 2 rows and vertical line in every 5 cols.
- I can do both of them separately. The problem is I can't get them both at the same time.
- If I change the text first, the borders of the affected cells have disappeared.
- If I draw the border first, the text colors change back to black(default).
- I want to change the text color and border color of the cells.

- My Codes are as follows: (I call these methods from grid cellpainting event)

Private Sub GridBorderLine(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
    'Declare Border Style and Border Color
    Dim vBStyle As ButtonBorderStyle = ButtonBorderStyle.Solid
    Dim vBColor As Color = Color.Black
    'Custom Row Border (Horizontal)
    If e.RowIndex Mod 2 = False AndAlso e.RowIndex > 0 Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentBackground)
        Dim H As Integer = 0
        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, vBColor, 0, vBStyle, vBColor, 3, vBStyle, vBColor, 0, vBStyle, vBColor, 0, vBStyle)
        e.Handled = True
    End If
    'Custom Column Border (Vertical)
    If e.ColumnIndex Mod 5 = False AndAlso e.ColumnIndex > 0 Then
         e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentBackground)
        Dim W As Integer = 0
        If e.RowIndex Mod 2 = False And e.RowIndex > 0 Then W = 3 Else W = 0
        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, vBColor, 3, vBStyle, vBColor, W, vBStyle, vBColor, 0, vBStyle, vBColor, 0, vBStyle)
        e.Handled = True
    End If
    'Custom Cell Color
    If e.FormattedValue.ToString.Contains(txt.Text) Then 
    End If
End Sub
Private Sub CellColor(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
    Dim vLength as Integer = txt.Text.Length
    e.Paint(e.CellBounds, DataGridViewPaintParts.All - DataGridViewPaintParts.ContentForeground)
    If e.FormattedValue.ToString = "" Then Exit Sub
    Dim vBeginning As String = e.FormattedValue.ToString.Substring(0, vLength)
    Dim vEnd As String = e.FormattedValue.ToString.Substring(vLength)
    Dim s As Size = TextRenderer.MeasureText(e.FormattedValue.ToString, vFont)
    Dim f As Size = TextRenderer.MeasureText(vBeginning, vFont)
    Dim b As Size = TextRenderer.MeasureText(vEnd, vFont)
    'Arrange Center Middle
    Dim w = e.CellBounds.X + ((e.CellBounds.Width - s.Width) / 2)
    Dim h = e.CellBounds.Y + ((e.CellBounds.Height - s.Height) / 2)
    Dim topLeft As New Point(w, h)
    Dim p As New Point(topLeft.X + (s.Width - b.Width), topLeft.Y)
   TextRenderer.DrawText(e.Graphics, vBeginning, vFont, topLeft, Color.Red)
    TextRenderer.DrawText(e.Graphics, vEnd, vFont, p, Color.Blue)
    e.Handled = True
End Sub
Posted 28-Dec-12 6:00am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,130
1 OriginalGriff 7,477
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084

Advertise | Privacy | Mobile
Web01 | 2.8.150427.4 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100