Preview DataGridView

Hi there,
Very nice sample to preview your data grid view !
As the Form is complex so i'll give the designer code

The Form Code

Public Class Form1
Private oStringFormat As StringFormat

Private oStringFormatComboBox As StringFormat

Private oButton As Button

Private oCheckbox As CheckBox

Private oComboBox As ComboBox

Private nTotalWidth As Int16

Private nRowPos As Int16

Private NewPage As Boolean

Private nPageNo As Int16

Private Header As String = "Header Test"

Private sUserName As String = "Will"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.PrintDocument1.Print()

Me.PrintPreviewDialog1.Document = Me.PrintDocument1

Me.PrintPreviewDialog1.Show()



End Sub

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
oStringFormat = New StringFormat
oStringFormat.Alignment = StringAlignment.Near

oStringFormat.LineAlignment = StringAlignment.Center

oStringFormat.Trimming = StringTrimming.EllipsisCharacter

oStringFormatComboBox = New StringFormat
oStringFormatComboBox.LineAlignment = StringAlignment.Center

oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap

oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter

oButton = New Button
oCheckbox = New CheckBox
oComboBox = New ComboBox
nTotalWidth = 0


For Each oColumn As DataGridViewColumn In dgvTest.Columns
nTotalWidth += oColumn.Width


Next
nPageNo = 1

NewPage = True
nRowPos = 0


End Sub



Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Static oColumnLefts As New ArrayList

Static oColumnWidths As New ArrayList

Static oColumnTypes As New ArrayList

Static nHeight As Int16

Dim nWidth, i, nRowsPerPage As Int16

Dim nTop As Int16 = e.MarginBounds.Top

Dim nLeft As Int16 = e.MarginBounds.Left

If nPageNo = 1 Then

For Each oColumn As DataGridViewColumn In dgvTest.Columns
nWidth = CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)
nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11

oColumnLefts.Add(nLeft)

oColumnWidths.Add(nWidth)

oColumnTypes.Add(oColumn.GetType)

nLeft += nWidth


Next

End If

Do While nRowPos < dgvTest.Rows.Count - 1

Dim oRow As DataGridViewRow = dgvTest.Rows(nRowPos)

If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then
DrawFooter(e, nRowsPerPage)

NewPage = True
nPageNo += 1

e.HasMorePages = True

Exit Sub

Else

If NewPage Then

' Draw Header
e.Graphics.DrawString(Header, New Font(dgvTest.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(dgvTest.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

' Draw Columns
nTop = e.MarginBounds.Top

i = 0


For Each oColumn As DataGridViewColumn In dgvTest.Columns
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font, New SolidBrush(oColumn.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
i += 1


Next
NewPage = False

End If
nTop += nHeight

i = 0


For Each oCell As DataGridViewCell In oRow.Cells

If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)

ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
oButton.Text = oCell.Value.ToString

oButton.Size = New Size(oColumnWidths(i), nHeight)

Dim oBitmap As New Bitmap(oButton.Width, oButton.Height)
oButton.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

ElseIf oColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then
oCheckbox.Size = New Size(14, 14)
oCheckbox.Checked = CType(oCell.Value, Boolean)

Dim oBitmap As New Bitmap(oColumnWidths(i), nHeight)

Dim oTempGraphics As Graphics = Graphics.FromImage(oBitmap)
oTempGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
oCheckbox.DrawToBitmap(oBitmap, New Rectangle(CType((oBitmap.Width - oCheckbox.Width) / 2, Int32), CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))

ElseIf oColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then
oComboBox.Size = New Size(oColumnWidths(i), nHeight)

Dim oBitmap As New Bitmap(oComboBox.Width, oComboBox.Height)
oComboBox.DrawToBitmap(oBitmap, New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))
e.Graphics.DrawImage(oBitmap, New Point(oColumnLefts(i), nTop))
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)

ElseIf oColumnTypes(i) Is GetType(DataGridViewImageColumn) Then

Dim oCellSize As Rectangle = New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)

Dim oImageSize As Size = CType(oCell.Value, Image).Size
e.Graphics.DrawImage(oCell.Value, New Rectangle(oColumnLefts(i) + CType(((oCellSize.Width - oImageSize.Width ) / 2), Int32), nTop + CType(((oCellSize.Height - oImageSize.Height) / 2), Int32), CType(oCell.Value, Image).Width, CType(oCell.Value, Image).Height))

End If
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))
i += 1


Next

End If
nRowPos += 1

nRowsPerPage += 1


Loop
DrawFooter(e, nRowsPerPage)

e.HasMorePages = False

End Sub

Private Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)

Dim sPageNo As String = nPageNo.ToString + " of " + Math.Ceiling(dgvTest.Rows.Count / RowsPerPage).ToString

' Right Align - User Name
e.Graphics.DrawString(sUserName, dgvTest.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, dgvTest.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)


' Left Align - Date/Time
e.Graphics.DrawString(Now.ToLongDateString + " " + Now.ToShortTimeString, dgvTest.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)

' Center - Page No. Info
e.Graphics.DrawString(sPageNo, dgvTest.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, dgvTest.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)




End Sub

End Class


The Designer Code


_
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
_
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog
Me.Button1 = New System.Windows.Forms.Button
Me.dgvtest = New System.Windows.Forms.DataGridView
Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn
CType(Me.dgvtest, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PrintDocument1
'
'
'PrintPreviewDialog1
'
Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
Me.PrintPreviewDialog1.Enabled = True
Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
Me.PrintPreviewDialog1.Visible = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(13, 279)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'dgvtest
'
Me.dgvtest.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.dgvtest.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2, Me.Column3})
Me.dgvtest.Location = New System.Drawing.Point(44, 28)
Me.dgvtest.Name = "dgvtest"
Me.dgvtest.RowTemplate.Height = 24
Me.dgvtest.Size = New System.Drawing.Size(240, 150)
Me.dgvtest.TabIndex = 1
'
'Column1
'
Me.Column1.HeaderText = "Column1"
Me.Column1.Name = "Column1"
'
'Column2
'
Me.Column2.HeaderText = "Column2"
Me.Column2.Name = "Column2"
'
'Column3
'
Me.Column3.HeaderText = "Column3"
Me.Column3.Name = "Column3"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(360, 314)
Me.Controls.Add(Me.dgvtest)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.dgvtest, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents dgvtest As System.Windows.Forms.DataGridView
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column3 As System.Windows.Forms.DataGridViewTextBoxColumn

End Class


hope it works and helps ::::)

0 comments:

Post a Comment