I've written a dervied DataGridColumnStyle class (inherits from DataGridTextBoxColumn) and overridden the Paint member so that i can specifiy which side of the column a vertical line is drawn.

Like so:

VB Code:
  1. Public Sub New(ByVal left As Boolean, ByVal right As Boolean)
  2.         'constructor
  3.         drawLeft = left
  4.         drawRight = right
  5.     End Sub
  6.  
  7.   '**other subs that REQUIRE to be overridden
  8.   '** are included in the class but they don't do much so
  9.   '** i've not bothered to include them here
  10.  
  11.    Protected Overloads Overrides Sub Paint(ByVal g As _
  12.    System.Drawing.Graphics, ByVal bounds As _
  13.    System.Drawing.Rectangle, ByVal source As _
  14.    System.Windows.Forms.CurrencyManager, ByVal rowNum As _
  15.    Integer)
  16.  
  17.         Paint(g, bounds, source, rowNum)
  18.  
  19.     End Sub
  20.  
  21.     Protected Overloads Overrides Sub Paint(ByVal g As _
  22.     System.Drawing.Graphics, ByVal bounds As _
  23.     System.Drawing.Rectangle, ByVal source As _
  24.     System.Windows.Forms.CurrencyManager, ByVal rowNum As _
  25.     Integer, ByVal alignToRight As Boolean)
  26.  
  27.         Paint(g, bounds, source, rowNum, alignToRight)
  28.  
  29.     End Sub
  30.  
  31.     Protected Overloads Overrides Sub Paint(ByVal g As _
  32.     System.Drawing.Graphics, ByVal bounds As _
  33.     System.Drawing.Rectangle, ByVal source As _
  34.     System.Windows.Forms.CurrencyManager, ByVal rowNum As _
  35.     Integer, ByVal BackBrush As Brush, ByVal ForeBrush As Brush, _
  36.     ByVal AlignToRight As Boolean)
  37.         'pen
  38.         Dim p As Pen = New Pen(Color.Black)
  39.  
  40.         'font
  41.         Dim font As Font = New System.Drawing.Font("Arial", 9)
  42.  
  43.         'string
  44.         Dim str As String
  45.  
  46.         'coordinates used for drawing lines
  47.         '(origin points)
  48.         Dim x1 As Single
  49.         Dim y1 As Single
  50.  
  51.         'set start coordinates
  52.         x1 = bounds.Location.X
  53.         y1 = bounds.Location.Y
  54.  
  55.         g.FillRectangle(BackBrush, bounds)
  56.  
  57.         'draw the horizontal line
  58.         g.DrawLine(p, x1, y1, x1 + bounds.Width, y1)
  59.  
  60.         'draw vertical line on left if flag set
  61.         If drawLeft = True Then
  62.             g.DrawLine(p, x1, y1, x1, y1 + bounds.Height)
  63.         End If
  64.  
  65.         'draw vertical line on right if flag set
  66.         If drawRight = True Then
  67.             g.DrawLine(p, x1 + bounds.Width, y1, x1 + bounds.Width, y1 + bounds.Height)
  68.         End If
  69.  
  70.         str = GetColumnValueATrow(source, rowNum)
  71.  
  72.         'draw the text
  73.         g.DrawString(str, font, Brushes.Black, bounds.X, bounds.Y)
  74.  
  75.     End Sub

Then when i use the columnstyle with my tablestyle and datagrid i find that it does not draw the vertical lines. But if i remove the 'if' statement for the left vertical line it draws the line.

Also if remove the
VB Code:
  1. g.FillRectangle(BackBrush, bounds)

the vertical lines are visible BUT obviously every time i scroll down it overwrites what was there previously and makes a scribbled mess.


I Suppose a possible solution is to create two classes, one for both sides. But surely this can be achieved with one class?

Any ideas where i'm going wrong?