|
Users often ask for the ability to display the gridlines when previewing and printing the grid. This is possible by adding borders to the SharpGrid cells before the preview, and removing the borders after the preview.
Option Explicit
Private Sub Command1_Click()
Dim row As SGRow
Dim col As SGColumn
With SGGrid1
If Command1.Caption = "Preview" Then
'adjust the cell borders for for each cell so we can see them in the print preview
For Each row In SGGrid1.Rows
For Each col In SGGrid1.Columns
row.Cells(col.Position).Style.BorderSize = 1
row.Cells(col.Position).Style.Borders = sgCellBorderAll
row.Cells(col.Position).Style.BorderColor = RGB(211, 211, 211) 'set the border colors to a gray color
Next
Next
'resize the columns after applying our cellborders
SGGrid1.AutoSizeColumns -1, -1
'set up the print preview
Set .PrintSettings.Viewer = arv
.PrintSettings.PrintGrid
Command1.Caption = "Grid"
arv.Visible = True
ElseIf Command1.Caption = "Grid" Then
'change the cell borders back since we are returning to the grid
For Each row In SGGrid1.Rows
For Each col In SGGrid1.Columns
row.Cells(col.Position).Style.Borders = sgCellNoBorder
Next
Next
'resize the columns after removing our cellborders
SGGrid1.AutoSizeColumns -1, -1
'return to view the grid
arv.Visible = False
Command1.Caption = "Preview"
End If
End With
End Sub
Private Sub Form_Load()
Dim ar As SGArray
Dim i, j As Long
'set up grid and add sample data
SGGrid1.DataRowCount = 10
SGGrid1.DataColCount = 10
Set ar = SGGrid1.Array
For i = 0 To ar.RowCount - 1
For j = 0 To ar.ColCount - 1
ar.Value(i, j) = i + j
Next
Next
End Sub
Private Sub Form_Resize()
'resize the viewer to the same size as SharpGrid
arv.Move SGGrid1.Left, SGGrid1.Top, SGGrid1.Width, SGGrid1.Height
End Sub
The attached sample demonstrates the use of the code above to simulate gridlines when previewing and printing the grid. The links below contain additional information about the use of the BorderSize, Borders, and BorderColor properties.
BorderSize
Borders
BorderColor
Applies To: SharpGrid 2.0
|