Ciao ragazzi,
qui di seguito vi posto il codice che utilizzo per richiamare l'anteprima di un DataGriView:
codice:
If newpage Then
row = Form4.dtg_profilo_agente.Rows(mRow)
x = e.MarginBounds.Left
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.FillRectangle(Brushes.LightGray, rc)
e.Graphics.DrawRectangle(Pens.Black, rc)
Select Case Form4.dtg_profilo_agente.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.MiddleRight
fmt.Alignment = StringAlignment.Far
rc.Offset(-1, 0)
Case DataGridViewContentAlignment.BottomCenter,
DataGridViewContentAlignment.MiddleCenter
fmt.Alignment = StringAlignment.Center
Case Else
fmt.Alignment = StringAlignment.Near
rc.Offset(2, 0)
End Select
e.Graphics.DrawString(Form4.dtg_profilo_agente.Columns(cell.ColumnIndex).HeaderText,
Form4.dtg_profilo_agente.Font, Brushes.Black, rc, fmt)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
End If
newpage = False
Dim thisNDX As Int32
For thisNDX = mRow To Form4.dtg_profilo_agente.RowCount - 1
If Form4.dtg_profilo_agente.Rows(thisNDX).IsNewRow Then Exit For
row = Form4.dtg_profilo_agente.Rows(thisNDX)
x = e.MarginBounds.Left
h = 0
x = e.MarginBounds.Left
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc)
Select Case Form4.dtg_profilo_agente.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.MiddleRight
fmt.Alignment = StringAlignment.Far
rc.Offset(-1, 0)
Case DataGridViewContentAlignment.BottomCenter,
DataGridViewContentAlignment.MiddleCenter
fmt.Alignment = StringAlignment.Center
Case Else
fmt.Alignment = StringAlignment.Near
rc.Offset(2, 0)
End Select
e.Graphics.DrawString(cell.FormattedValue.ToString(),
Form4.dtg_profilo_agente.Font, Brushes.Black, rc, fmt)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
mRow = thisNDX + 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
newpage = True
Return
End If
Next
End If
Quello che devo fare è stampare il contenuto del documento PrintPreviewControl1
Grazie
SanMichele