Category / Section
How to draw the bottom and right border of the grid when pixel scroll is enabled?
1 min read
By default, the WinForms Grid will be scrolled pixel by pixel when VScrollPixel and HScrollPixel properties are enabled. So, the bottom border of the last row and right border of the last column will not be visible. In order to display the bottom border of last row and right border of last column, CellDrawn event can be used.
Code snippet
C#
//Event Triggering this.gridControl1.CellDrawn += new GridDrawCellEventHandler(gridControl1_CellDrawn); //Event Customization private void gridControl1_CellDrawn(object sender, GridDrawCellEventArgs e) { // Draw the bottom border. if (e.RowIndex == this.gridControl1.RowCount && this.gridControl1.VScrollPixel) { e.Graphics.DrawLine(new Pen(new SolidBrush(this.gridControl1.GridLineColor), 1.5f) { DashStyle = DashStyle.Solid }, e.Bounds.Left, e.Bounds.Bottom - 2, e.Bounds.Right - 1, e.Bounds.Bottom - 2); } //Draw the right border. if (e.ColIndex == this.gridControl1.ColCount && this.gridControl1.HScrollPixel) { e.Graphics.DrawLine(new Pen(new SolidBrush(this.gridControl1.GridLineColor), 1.5f) { DashStyle = DashStyle.Solid }, e.Bounds.Right - 2, e.Bounds.Top, e.Bounds.Right - 2, e.Bounds.Bottom); } }
VB
'Event Triggering AddHandler gridControl1.CellDrawn, AddressOf gridControl1_CellDrawn 'Event Customization Private Sub gridControl1_CellDrawn(ByVal sender As Object, ByVal e As GridDrawCellEventArgs) ' Draw the bottom border. If e.RowIndex = Me.gridControl1.RowCount AndAlso Me.gridControl1.VScrollPixel Then e.Graphics.DrawLine(New Pen(New SolidBrush(Me.gridControl1.GridLineColor), 1.5f) With {.DashStyle = DashStyle.Solid}, e.Bounds.Left, e.Bounds.Bottom - 2, e.Bounds.Right - 1, e.Bounds.Bottom - 2) End If 'Draw the right border. If e.ColIndex = Me.gridControl1.ColCount AndAlso Me.gridControl1.HScrollPixel Then e.Graphics.DrawLine(New Pen(New SolidBrush(Me.gridControl1.GridLineColor), 1.5f) With {.DashStyle = DashStyle.Solid}, e.Bounds.Right - 2, e.Bounds.Top, e.Bounds.Right - 2, e.Bounds.Bottom) End If End Sub
Screenshot
Sample links:
C#: CellBorder_CS
VB: CellBorder_VB