Click here to Skip to main content
Rate this: bad
good
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 
        CellColor(e)
    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
korobin232

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 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
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