EDN Admin
Well-known member
hi evey
plz help again.
im using that code but little bit problem plz solve my prob
the prob is that
suppose i have 40 datagirdview rows
i want print 10 rows pr page example 10 rows 1st page 10 rows 2nd page 10 rows third page and 10 rows 4th page
how can i print and where i can put the code
help everyone
Imports System.Data.SqlClient<br/>
Imports System.Data<br/>
Imports System.Collections.Generic<br/>
Public Class PrintDGV<br/>
<br/>
Private Shared StrFormat As StringFormat Holds content of a TextBox Cell to write by DrawString<br/>
Private Shared StrFormatComboBox As StringFormat Holds content of a Boolean Cell to write by DrawImage<br/>
Private Shared CellButton As Button Holds the Contents of Button Cell<br/>
Private Shared CellCheckBox As CheckBox Holds the Contents of CheckBox Cell<br/>
Private Shared CellComboBox As ComboBox Holds the Contents of ComboBox Cell<br/>
Private Shared TotalWidth As Int16 Summation of Columns widths<br/>
Private Shared RowPos As Int16 Position of currently printing row
<br/>
Private Shared NewPage As Boolean Indicates if a new page reached
<br/>
Private Shared PageNo As Int16 Number of pages to print
<br/>
Private Shared ColumnLefts As New ArrayList Left Coordinate of Columns<br/>
Private Shared ColumnWidths As New ArrayList Width of Columns<br/>
Private Shared ColumnTypes As New ArrayList DataType of Columns<br/>
Private Shared CellHeight As Int16 Height of DataGrid Cell<br/>
Private Shared RowsPerPage As Int16 Number of Rows per Page
<br/>
Private Shared WithEvents PrintDoc As New System.Drawing.Printing.PrintDocument PrintDocumnet Object used for printing<br/>
Private Shared PrintTitle As String = "" Header of pages<br/>
Private Shared dgv As DataGridView Holds DataGrid Object to print its contents<br/>
Private Shared SelectedColumns As New List(Of String) The Columns Selected by user to print.<br/>
Private Shared AvailableColumns As New List(Of String) All Columns avaiable in DataGrid
<br/>
Private Shared PrintAllRows As Boolean = True True = print all rows, False = print selected rows
<br/>
Private Shared FitToPageWidth As Boolean = True True = Fits selected columns to page width , False = Print columns as showed
<br/>
Private Shared HeaderHeight As Int16 = 0<br/>
Private Shared value, a, b, c, d, f, g, h, i, j, k, l, m As String<br/>
Public Shared Sub Print_DataGridView(ByVal dgv1 As DataGridView)<br/>
Dim ppvw As PrintPreviewDialog<br/>
Try<br/>
Getting DataGridView object to print<br/>
dgv = dgv1<br/>
<br/>
Getting all Coulmns Names in the DataGridView<br/>
AvailableColumns.Clear()<br/>
<br/>
For Each c As DataGridViewColumn In Quote.DataGridView1.Columns<br/>
If Not c.Visible Then Continue For<br/>
AvailableColumns.Add(c.HeaderText)<br/>
Next<br/>
<br/>
Showing the PrintOption Form<br/>
Dim dlg As New PrintOptions(AvailableColumns)<br/>
If dlg.ShowDialog() <> DialogResult.OK Then Exit Sub<br/>
<br/>
Saving some printing attributes<br/>
PrintTitle = dlg.PrintTitle<br/>
PrintAllRows = dlg.PrintAllRows<br/>
FitToPageWidth = dlg.FitToPageWidth<br/>
SelectedColumns = dlg.GetSelectedColumns<br/>
RowsPerPage = 0<br/>
ppvw = New PrintPreviewDialog<br/>
ppvw.Document = PrintDoc<br/>
Showing the Print Preview Page<br/>
If ppvw.ShowDialog() <> DialogResult.OK Then Exit Sub<br/>
Printing the Documnet<br/>
PrintDoc.Print()<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
Private Shared Sub PrintDoc_BeginPrint(ByVal sender As Object, _<br/>
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDoc.BeginPrint<br/>
Dim value As String<br/>
Dim Con As New SqlConnection<br/>
Dim Da As New SqlDataAdapter<br/>
Dim Ds As New DataSet<br/>
Dim SQL As String<br/>
Con = New SqlConnection("Data Source=(local);Initial Catalog=purchase;Integrated Security=True")<br/>
Con.Open()<br/>
SQL = "Select * from Companydetails"<br/>
Da = New SqlDataAdapter(SQL, Con)<br/>
Da.SelectCommand.ExecuteNonQuery()<br/>
Da.Fill(Ds, "Companydetails")<br/>
a = Ds.Tables("companydetails").Rows(0).Item(0)<br/>
value = Ds.Tables("companydetails").Rows(0).Item(1)<br/>
b = Ds.Tables("companydetails").Rows(0).Item(2)<br/>
h = Ds.Tables("companydetails").Rows(0).Item(3)<br/>
i = Ds.Tables("companydetails").Rows(0).Item(4)<br/>
j = Ds.Tables("companydetails").Rows(0).Item(5)<br/>
k = Ds.Tables("companydetails").Rows(0).Item(6)<br/>
l = Ds.Tables("companydetails").Rows(0).Item(7)<br/>
m = Ds.Tables("companydetails").Rows(0).Item(9)<br/>
<br/>
SQL = "Select * from QuoteClientDetails"<br/>
Da = New SqlDataAdapter(SQL, Con)<br/>
Da.SelectCommand.ExecuteNonQuery()<br/>
Da.Fill(Ds, "QuoteClientDetails")<br/>
c = Ds.Tables("QuoteClientDetails").Rows(0).Item(12)<br/>
d = Ds.Tables("QuoteClientDetails").Rows(0).Item(13)<br/>
f = Ds.Tables("QuoteClientDetails").Rows(0).Item(15) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(16) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(17) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(18)
& "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(19)<br/>
g = Ds.Tables("QuoteClientDetails").Rows(0).Item(20)<br/>
Try<br/>
Formatting the Content of Text Cells to print<br/>
StrFormat = New StringFormat<br/>
StrFormat.Alignment = StringAlignment.Near<br/>
StrFormat.LineAlignment = StringAlignment.Center<br/>
StrFormat.Trimming = StringTrimming.EllipsisCharacter<br/>
<br/>
Formatting the Content of Combo Cells to print<br/>
StrFormatComboBox = New StringFormat<br/>
StrFormatComboBox.LineAlignment = StringAlignment.Center<br/>
StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap<br/>
StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter<br/>
<br/>
ColumnLefts.Clear()<br/>
ColumnWidths.Clear()<br/>
ColumnTypes.Clear()<br/>
CellHeight = 0<br/>
RowsPerPage = 0<br/>
<br/>
For various column types<br/>
CellButton = New Button<br/>
CellCheckBox = New CheckBox<br/>
CellComboBox = New ComboBox<br/>
<br/>
TotalWidth = 0<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For<br/>
TotalWidth += GridCol.Width<br/>
Next<br/>
PageNo = 1<br/>
NewPage = True<br/>
RowPos = 0<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
<br/>
Private Shared Sub PrintDoc_PrintPage(ByVal sender As Object, _<br/>
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage<br/>
<br/>
Dim tmpWidth As Int16, i As Int16<br/>
Dim tmpTop As Int16 = e.MarginBounds.Top<br/>
Dim tmpLeft As Int16 = e.MarginBounds.Left<br/>
Dim font As New Font("Verdana", 12, FontStyle.Bold)<br/>
Dim fontx As New Font("Verdand", 16, FontStyle.Regular)<br/>
Dim title As New Font("Verdana", 20, FontStyle.Regular)<br/>
Dim Mysimple As New Font("Verdana", 8, FontStyle.Regular)<br/>
Dim Mysimple1 As New Font("Verdana", 8, FontStyle.Bold)<br/>
Dim objright As Integer = 100<br/>
Dim x As Integer<br/>
Dim y As Integer<br/>
Try<br/>
Before starting first page, it saves Width & Height of Headers and CoulmnType<br/>
If PageNo = 1 Then<br/>
x = 50<br/>
y = 50<br/>
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)<br/>
e.Graphics.DrawString(value, title, Brushes.Black, x, y + 40)<br/>
e.Graphics.DrawString(a, fontx, Brushes.Black, x + 10, y + 75)<br/>
e.Graphics.DrawString("___________________________________________________________________", title, Brushes.Black, x - 55, y + 80)<br/>
e.Graphics.DrawLine(myPen, 20, 400, 20, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x - 35, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x - 35, y + 319)<br/>
e.Graphics.DrawLine(myPen, 268, 400, 268, 200)<br/>
<br/>
e.Graphics.DrawLine(myPen, 295, 400, 295, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 240, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 240, y + 319)<br/>
e.Graphics.DrawLine(myPen, 543, 400, 543, 200)<br/>
<br/>
e.Graphics.DrawLine(myPen, 573, 400, 573, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 519, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 519, y + 319)<br/>
e.Graphics.DrawLine(myPen, 822, 400, 822, 200)<br/>
e.Graphics.DrawString("Sold To:", font, Brushes.Black, x, y + 120)<br/>
e.Graphics.DrawString("Ship To:", font, Brushes.Black, x + 250, y + 120)<br/>
e.Graphics.DrawString(Quote.TextBox2.Text, Mysimple, Brushes.Black, x + 18, y + 160)<br/>
e.Graphics.DrawString("Name:", Mysimple1, Brushes.Black, x - 25, y + 160)<br/>
e.Graphics.DrawString("Company Name:", Mysimple1, Brushes.Black, x - 25, y + 200)<br/>
e.Graphics.DrawString(Quote.TextBox3.Text, Mysimple, Brushes.Black, x + 82, y + 200)<br/>
e.Graphics.DrawString("Address:", Mysimple1, Brushes.Black, x - 25, y + 240)<br/>
<br/>
e.Graphics.DrawString(Quote.RichTextBox1.Text, Mysimple, Brushes.Black, x + 35, y + 240)<br/>
<br/>
e.Graphics.DrawString("Phone No#:", Mysimple1, Brushes.Black, x - 25, y + 310)<br/>
e.Graphics.DrawString(Quote.TextBox4.Text, Mysimple, Brushes.Black, x + 55, y + 310)<br/>
e.Graphics.DrawString("Date:", Mysimple1, Brushes.Black, x + 530, y + 160)<br/>
e.Graphics.DrawString(Quote.DateTimePicker1.Value, Mysimple, Brushes.Black, x + 565, y + 160)<br/>
e.Graphics.DrawString("Reg No:", Mysimple1, Brushes.Black, x + 530, y + 190)<br/>
e.Graphics.DrawString(b, Mysimple, Brushes.Black, x + 585, y + 190)<br/>
e.Graphics.DrawString("Quote No:", Mysimple1, Brushes.Black, x + 530, y + 220)<br/>
e.Graphics.DrawString(Quote.TextBox5.Text, Mysimple, Brushes.Black, x + 595, y + 220)<br/>
e.Graphics.DrawString("Due Date:", Mysimple1, Brushes.Black, x + 530, y + 250)<br/>
e.Graphics.DrawString(Quote.DateTimePicker2.Value, Mysimple, Brushes.Black, x + 590, y + 250)<br/>
<br/>
ship<br/>
e.Graphics.DrawString("Name:", Mysimple1, Brushes.Black, x + 250, y + 160)<br/>
e.Graphics.DrawString(c, Mysimple, Brushes.Black, x + 293, y + 160)<br/>
e.Graphics.DrawString("Company Name:", Mysimple1, Brushes.Black, x + 250, y + 200)<br/>
e.Graphics.DrawString(d, Mysimple, Brushes.Black, x + 360, y + 200)<br/>
e.Graphics.DrawString("Address:", Mysimple1, Brushes.Black, x + 250, y + 240)<br/>
e.Graphics.DrawString(f, Mysimple, Brushes.Black, x + 310, y + 240)<br/>
e.Graphics.DrawString("Phone No#:", Mysimple1, Brushes.Black, x + 250, y + 310)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
<br/>
<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
Detemining whether the columns are fitted to page or not.<br/>
If FitToPageWidth Then<br/>
tmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _<br/>
TotalWidth * (e.MarginBounds.Width / TotalWidth)), Int16)<br/>
Else<br/>
tmpWidth = GridCol.Width<br/>
End If<br/>
HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _<br/>
GridCol.InheritedStyle.Font, tmpWidth).Height + 11<br/>
<br/>
ColumnLefts.Add(tmpLeft)<br/>
ColumnWidths.Add(tmpWidth)<br/>
ColumnTypes.Add(GridCol.GetType)<br/>
tmpLeft += tmpWidth<br/>
Next<br/>
End If<br/>
<br/>
Printing Current Page, Row by Row<br/>
Do While RowPos <= dgv.Rows.Count - 1<br/>
Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)<br/>
If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then<br/>
RowPos += 1 : Continue Do<br/>
End If<br/>
<br/>
CellHeight = GridRow.Height<br/>
<br/>
If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then<br/>
DrawFooter(e, RowsPerPage)<br/>
NewPage = True<br/>
PageNo += 1<br/>
e.HasMorePages = True<br/>
Exit Sub<br/>
Else<br/>
If NewPage Then<br/>
Draw Header<br/>
e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _<br/>
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _<br/>
e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _<br/>
FontStyle.Bold), e.MarginBounds.Width).Height - 13)<br/>
<br/>
Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString<br/>
<br/>
e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _<br/>
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _<br/>
e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _<br/>
e.MarginBounds.Width).Width), e.MarginBounds.Top - _<br/>
e.Graphics.MeasureString(PrintTitle, _<br/>
New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _<br/>
e.MarginBounds.Width).Height - 13)<br/>
<br/>
Draw Columns<br/>
tmpTop = e.MarginBounds.Y + 350<br/>
i = 0<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _<br/>
New Rectangle(ColumnLefts(i), tmpTop, ColumnWidths(i),
HeaderHeight))<br/>
<br/>
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), HeaderHeight))<br/>
<br/>
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _<br/>
New SolidBrush(GridCol.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i),
_<br/>
HeaderHeight), StrFormat)<br/>
i += 1<br/>
Next<br/>
NewPage = False<br/>
<br/>
tmpTop += HeaderHeight<br/>
End If<br/>
<br/>
i = 0<br/>
For Each Cel As DataGridViewCell In GridRow.Cells<br/>
If Not Cel.OwningColumn.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(Cel.OwningColumn.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
For the TextBox Column<br/>
If ColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse _<br/>
ColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then<br/>
<br/>
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _<br/>
New SolidBrush(Cel.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i),
_<br/>
CellHeight), StrFormat)<br/>
<br/>
For the Button Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then<br/>
<br/>
CellButton.Text = Cel.Value.ToString<br/>
CellButton.Size = New Size(ColumnWidths(i), CellHeight)<br/>
Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)<br/>
CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
<br/>
For the CheckBox Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then<br/>
<br/>
CellCheckBox.Size = New Size(14, 14)<br/>
CellCheckBox.Checked = CType(Cel.Value, Boolean)<br/>
Dim bmp As New Bitmap(ColumnWidths(i), CellHeight)<br/>
Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)<br/>
tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _<br/>
CellCheckBox.Width) / 2, Int32), CType((bmp.Height
- _<br/>
CellCheckBox.Height) / 2, Int32), CellCheckBox.Width,
_<br/>
CellCheckBox.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
<br/>
For the ComboBox Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then<br/>
<br/>
CellComboBox.Size = New Size(ColumnWidths(i), CellHeight)<br/>
Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)<br/>
CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _<br/>
New SolidBrush(Cel.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i) + 1, tmpTop, ColumnWidths(i)
_<br/>
- 16, CellHeight), StrFormatComboBox)<br/>
<br/>
For the Image Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewImageColumn) Then<br/>
<br/>
Dim CelSize As Rectangle = New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), CellHeight)<br/>
Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size<br/>
e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLefts(i) _<br/>
+ CType(((CelSize.Width - ImgSize.Width) / 2),
Int32), _<br/>
tmpTop + CType(((CelSize.Height - ImgSize.Height)
/ 2), _<br/>
Int32), CType(Cel.FormattedValue, Image).Width,
CType(Cel.FormattedValue, _<br/>
Image).Height))<br/>
<br/>
End If<br/>
<br/>
Drawing Cells Borders
<br/>
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), CellHeight))<br/>
<br/>
i += 1<br/>
<br/>
Next<br/>
tmpTop += CellHeight<br/>
<br/>
End If<br/>
<br/>
RowPos += 1<br/>
For the first page it calculates Rows per Page<br/>
If PageNo = 1 Then<br/>
RowsPerPage += 1<br/>
End If<br/>
Loop<br/>
<br/>
If RowsPerPage = 0 Then Exit Sub<br/>
<br/>
<br/>
<br/>
<br/>
DrawFooter(e, RowsPerPage)<br/>
<br/>
e.HasMorePages = False<br/>
e.Graphics.DrawString("total", Mysimple1, Brushes.Black, x + 60, y + 910)<br/>
e.Graphics.DrawString(c, Mysimple, Brushes.Black, x, y)<br/>
e.Graphics.DrawString("Discount", Mysimple1, Brushes.Black, x + 250, y + 920)<br/>
e.Graphics.DrawString(d, Mysimple, Brushes.Black, x + 360, y + 990)<br/>
e.Graphics.DrawString("Shiping Charges", Mysimple1, Brushes.Black, x + 250, y + 930)<br/>
e.Graphics.DrawString(f, Mysimple, Brushes.Black, x + 310, y + 990)<br/>
e.Graphics.DrawString("Sales Tax", Mysimple1, Brushes.Black, x + 250, y + 940)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
e.Graphics.DrawString("Sales Tax", Mysimple1, Brushes.Black, x + 250, y + 950)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
<br/>
Private Shared Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)<br/>
Dim cnt As Integer<br/>
<br/>
Detemining rows number to print<br/>
If PrintAllRows Then<br/>
If dgv.Rows(dgv.Rows.Count - 1).IsNewRow Then<br/>
When the DataGridView doesnt allow adding rows<br/>
cnt = dgv.Rows.Count - 2<br/>
Else<br/>
When the DataGridView allows adding rows<br/>
cnt = dgv.Rows.Count - 1<br/>
End If<br/>
Else<br/>
cnt = dgv.SelectedRows.Count<br/>
End If<br/>
<br/>
Writing the Page Number on the Bottom of Page<br/>
Dim PageNum As String = PageNo.ToString + " of " + _<br/>
Math.Ceiling(cnt / RowsPerPage).ToString<br/>
e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, _<br/>
e.MarginBounds.Left + (e.MarginBounds.Width - _<br/>
e.Graphics.MeasureString(PageNum, dgv.Font, _<br/>
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + _<br/>
e.MarginBounds.Height + 31)<br/>
<br/>
End Sub<br/>
<br/>
End Class
View the full article
plz help again.
im using that code but little bit problem plz solve my prob
the prob is that
suppose i have 40 datagirdview rows
i want print 10 rows pr page example 10 rows 1st page 10 rows 2nd page 10 rows third page and 10 rows 4th page
how can i print and where i can put the code
help everyone
Imports System.Data.SqlClient<br/>
Imports System.Data<br/>
Imports System.Collections.Generic<br/>
Public Class PrintDGV<br/>
<br/>
Private Shared StrFormat As StringFormat Holds content of a TextBox Cell to write by DrawString<br/>
Private Shared StrFormatComboBox As StringFormat Holds content of a Boolean Cell to write by DrawImage<br/>
Private Shared CellButton As Button Holds the Contents of Button Cell<br/>
Private Shared CellCheckBox As CheckBox Holds the Contents of CheckBox Cell<br/>
Private Shared CellComboBox As ComboBox Holds the Contents of ComboBox Cell<br/>
Private Shared TotalWidth As Int16 Summation of Columns widths<br/>
Private Shared RowPos As Int16 Position of currently printing row
<br/>
Private Shared NewPage As Boolean Indicates if a new page reached
<br/>
Private Shared PageNo As Int16 Number of pages to print
<br/>
Private Shared ColumnLefts As New ArrayList Left Coordinate of Columns<br/>
Private Shared ColumnWidths As New ArrayList Width of Columns<br/>
Private Shared ColumnTypes As New ArrayList DataType of Columns<br/>
Private Shared CellHeight As Int16 Height of DataGrid Cell<br/>
Private Shared RowsPerPage As Int16 Number of Rows per Page
<br/>
Private Shared WithEvents PrintDoc As New System.Drawing.Printing.PrintDocument PrintDocumnet Object used for printing<br/>
Private Shared PrintTitle As String = "" Header of pages<br/>
Private Shared dgv As DataGridView Holds DataGrid Object to print its contents<br/>
Private Shared SelectedColumns As New List(Of String) The Columns Selected by user to print.<br/>
Private Shared AvailableColumns As New List(Of String) All Columns avaiable in DataGrid
<br/>
Private Shared PrintAllRows As Boolean = True True = print all rows, False = print selected rows
<br/>
Private Shared FitToPageWidth As Boolean = True True = Fits selected columns to page width , False = Print columns as showed
<br/>
Private Shared HeaderHeight As Int16 = 0<br/>
Private Shared value, a, b, c, d, f, g, h, i, j, k, l, m As String<br/>
Public Shared Sub Print_DataGridView(ByVal dgv1 As DataGridView)<br/>
Dim ppvw As PrintPreviewDialog<br/>
Try<br/>
Getting DataGridView object to print<br/>
dgv = dgv1<br/>
<br/>
Getting all Coulmns Names in the DataGridView<br/>
AvailableColumns.Clear()<br/>
<br/>
For Each c As DataGridViewColumn In Quote.DataGridView1.Columns<br/>
If Not c.Visible Then Continue For<br/>
AvailableColumns.Add(c.HeaderText)<br/>
Next<br/>
<br/>
Showing the PrintOption Form<br/>
Dim dlg As New PrintOptions(AvailableColumns)<br/>
If dlg.ShowDialog() <> DialogResult.OK Then Exit Sub<br/>
<br/>
Saving some printing attributes<br/>
PrintTitle = dlg.PrintTitle<br/>
PrintAllRows = dlg.PrintAllRows<br/>
FitToPageWidth = dlg.FitToPageWidth<br/>
SelectedColumns = dlg.GetSelectedColumns<br/>
RowsPerPage = 0<br/>
ppvw = New PrintPreviewDialog<br/>
ppvw.Document = PrintDoc<br/>
Showing the Print Preview Page<br/>
If ppvw.ShowDialog() <> DialogResult.OK Then Exit Sub<br/>
Printing the Documnet<br/>
PrintDoc.Print()<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
Private Shared Sub PrintDoc_BeginPrint(ByVal sender As Object, _<br/>
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDoc.BeginPrint<br/>
Dim value As String<br/>
Dim Con As New SqlConnection<br/>
Dim Da As New SqlDataAdapter<br/>
Dim Ds As New DataSet<br/>
Dim SQL As String<br/>
Con = New SqlConnection("Data Source=(local);Initial Catalog=purchase;Integrated Security=True")<br/>
Con.Open()<br/>
SQL = "Select * from Companydetails"<br/>
Da = New SqlDataAdapter(SQL, Con)<br/>
Da.SelectCommand.ExecuteNonQuery()<br/>
Da.Fill(Ds, "Companydetails")<br/>
a = Ds.Tables("companydetails").Rows(0).Item(0)<br/>
value = Ds.Tables("companydetails").Rows(0).Item(1)<br/>
b = Ds.Tables("companydetails").Rows(0).Item(2)<br/>
h = Ds.Tables("companydetails").Rows(0).Item(3)<br/>
i = Ds.Tables("companydetails").Rows(0).Item(4)<br/>
j = Ds.Tables("companydetails").Rows(0).Item(5)<br/>
k = Ds.Tables("companydetails").Rows(0).Item(6)<br/>
l = Ds.Tables("companydetails").Rows(0).Item(7)<br/>
m = Ds.Tables("companydetails").Rows(0).Item(9)<br/>
<br/>
SQL = "Select * from QuoteClientDetails"<br/>
Da = New SqlDataAdapter(SQL, Con)<br/>
Da.SelectCommand.ExecuteNonQuery()<br/>
Da.Fill(Ds, "QuoteClientDetails")<br/>
c = Ds.Tables("QuoteClientDetails").Rows(0).Item(12)<br/>
d = Ds.Tables("QuoteClientDetails").Rows(0).Item(13)<br/>
f = Ds.Tables("QuoteClientDetails").Rows(0).Item(15) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(16) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(17) & "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(18)
& "," & Ds.Tables("QuoteClientDetails").Rows(0).Item(19)<br/>
g = Ds.Tables("QuoteClientDetails").Rows(0).Item(20)<br/>
Try<br/>
Formatting the Content of Text Cells to print<br/>
StrFormat = New StringFormat<br/>
StrFormat.Alignment = StringAlignment.Near<br/>
StrFormat.LineAlignment = StringAlignment.Center<br/>
StrFormat.Trimming = StringTrimming.EllipsisCharacter<br/>
<br/>
Formatting the Content of Combo Cells to print<br/>
StrFormatComboBox = New StringFormat<br/>
StrFormatComboBox.LineAlignment = StringAlignment.Center<br/>
StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap<br/>
StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter<br/>
<br/>
ColumnLefts.Clear()<br/>
ColumnWidths.Clear()<br/>
ColumnTypes.Clear()<br/>
CellHeight = 0<br/>
RowsPerPage = 0<br/>
<br/>
For various column types<br/>
CellButton = New Button<br/>
CellCheckBox = New CheckBox<br/>
CellComboBox = New ComboBox<br/>
<br/>
TotalWidth = 0<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then Continue For<br/>
TotalWidth += GridCol.Width<br/>
Next<br/>
PageNo = 1<br/>
NewPage = True<br/>
RowPos = 0<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
<br/>
Private Shared Sub PrintDoc_PrintPage(ByVal sender As Object, _<br/>
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage<br/>
<br/>
Dim tmpWidth As Int16, i As Int16<br/>
Dim tmpTop As Int16 = e.MarginBounds.Top<br/>
Dim tmpLeft As Int16 = e.MarginBounds.Left<br/>
Dim font As New Font("Verdana", 12, FontStyle.Bold)<br/>
Dim fontx As New Font("Verdand", 16, FontStyle.Regular)<br/>
Dim title As New Font("Verdana", 20, FontStyle.Regular)<br/>
Dim Mysimple As New Font("Verdana", 8, FontStyle.Regular)<br/>
Dim Mysimple1 As New Font("Verdana", 8, FontStyle.Bold)<br/>
Dim objright As Integer = 100<br/>
Dim x As Integer<br/>
Dim y As Integer<br/>
Try<br/>
Before starting first page, it saves Width & Height of Headers and CoulmnType<br/>
If PageNo = 1 Then<br/>
x = 50<br/>
y = 50<br/>
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)<br/>
e.Graphics.DrawString(value, title, Brushes.Black, x, y + 40)<br/>
e.Graphics.DrawString(a, fontx, Brushes.Black, x + 10, y + 75)<br/>
e.Graphics.DrawString("___________________________________________________________________", title, Brushes.Black, x - 55, y + 80)<br/>
e.Graphics.DrawLine(myPen, 20, 400, 20, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x - 35, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x - 35, y + 319)<br/>
e.Graphics.DrawLine(myPen, 268, 400, 268, 200)<br/>
<br/>
e.Graphics.DrawLine(myPen, 295, 400, 295, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 240, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 240, y + 319)<br/>
e.Graphics.DrawLine(myPen, 543, 400, 543, 200)<br/>
<br/>
e.Graphics.DrawLine(myPen, 573, 400, 573, 200)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 519, y + 119)<br/>
e.Graphics.DrawString("______________", title, Brushes.Black, x + 519, y + 319)<br/>
e.Graphics.DrawLine(myPen, 822, 400, 822, 200)<br/>
e.Graphics.DrawString("Sold To:", font, Brushes.Black, x, y + 120)<br/>
e.Graphics.DrawString("Ship To:", font, Brushes.Black, x + 250, y + 120)<br/>
e.Graphics.DrawString(Quote.TextBox2.Text, Mysimple, Brushes.Black, x + 18, y + 160)<br/>
e.Graphics.DrawString("Name:", Mysimple1, Brushes.Black, x - 25, y + 160)<br/>
e.Graphics.DrawString("Company Name:", Mysimple1, Brushes.Black, x - 25, y + 200)<br/>
e.Graphics.DrawString(Quote.TextBox3.Text, Mysimple, Brushes.Black, x + 82, y + 200)<br/>
e.Graphics.DrawString("Address:", Mysimple1, Brushes.Black, x - 25, y + 240)<br/>
<br/>
e.Graphics.DrawString(Quote.RichTextBox1.Text, Mysimple, Brushes.Black, x + 35, y + 240)<br/>
<br/>
e.Graphics.DrawString("Phone No#:", Mysimple1, Brushes.Black, x - 25, y + 310)<br/>
e.Graphics.DrawString(Quote.TextBox4.Text, Mysimple, Brushes.Black, x + 55, y + 310)<br/>
e.Graphics.DrawString("Date:", Mysimple1, Brushes.Black, x + 530, y + 160)<br/>
e.Graphics.DrawString(Quote.DateTimePicker1.Value, Mysimple, Brushes.Black, x + 565, y + 160)<br/>
e.Graphics.DrawString("Reg No:", Mysimple1, Brushes.Black, x + 530, y + 190)<br/>
e.Graphics.DrawString(b, Mysimple, Brushes.Black, x + 585, y + 190)<br/>
e.Graphics.DrawString("Quote No:", Mysimple1, Brushes.Black, x + 530, y + 220)<br/>
e.Graphics.DrawString(Quote.TextBox5.Text, Mysimple, Brushes.Black, x + 595, y + 220)<br/>
e.Graphics.DrawString("Due Date:", Mysimple1, Brushes.Black, x + 530, y + 250)<br/>
e.Graphics.DrawString(Quote.DateTimePicker2.Value, Mysimple, Brushes.Black, x + 590, y + 250)<br/>
<br/>
ship<br/>
e.Graphics.DrawString("Name:", Mysimple1, Brushes.Black, x + 250, y + 160)<br/>
e.Graphics.DrawString(c, Mysimple, Brushes.Black, x + 293, y + 160)<br/>
e.Graphics.DrawString("Company Name:", Mysimple1, Brushes.Black, x + 250, y + 200)<br/>
e.Graphics.DrawString(d, Mysimple, Brushes.Black, x + 360, y + 200)<br/>
e.Graphics.DrawString("Address:", Mysimple1, Brushes.Black, x + 250, y + 240)<br/>
e.Graphics.DrawString(f, Mysimple, Brushes.Black, x + 310, y + 240)<br/>
e.Graphics.DrawString("Phone No#:", Mysimple1, Brushes.Black, x + 250, y + 310)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
<br/>
<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
Detemining whether the columns are fitted to page or not.<br/>
If FitToPageWidth Then<br/>
tmpWidth = CType(Math.Floor(GridCol.Width / TotalWidth * _<br/>
TotalWidth * (e.MarginBounds.Width / TotalWidth)), Int16)<br/>
Else<br/>
tmpWidth = GridCol.Width<br/>
End If<br/>
HeaderHeight = e.Graphics.MeasureString(GridCol.HeaderText, _<br/>
GridCol.InheritedStyle.Font, tmpWidth).Height + 11<br/>
<br/>
ColumnLefts.Add(tmpLeft)<br/>
ColumnWidths.Add(tmpWidth)<br/>
ColumnTypes.Add(GridCol.GetType)<br/>
tmpLeft += tmpWidth<br/>
Next<br/>
End If<br/>
<br/>
Printing Current Page, Row by Row<br/>
Do While RowPos <= dgv.Rows.Count - 1<br/>
Dim GridRow As DataGridViewRow = dgv.Rows(RowPos)<br/>
If GridRow.IsNewRow OrElse (Not PrintAllRows AndAlso Not GridRow.Selected) Then<br/>
RowPos += 1 : Continue Do<br/>
End If<br/>
<br/>
CellHeight = GridRow.Height<br/>
<br/>
If tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top Then<br/>
DrawFooter(e, RowsPerPage)<br/>
NewPage = True<br/>
PageNo += 1<br/>
e.HasMorePages = True<br/>
Exit Sub<br/>
Else<br/>
If NewPage Then<br/>
Draw Header<br/>
e.Graphics.DrawString(PrintTitle, New Font(dgv.Font, FontStyle.Bold), _<br/>
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - _<br/>
e.Graphics.MeasureString(PrintTitle, New Font(dgv.Font, _<br/>
FontStyle.Bold), e.MarginBounds.Width).Height - 13)<br/>
<br/>
Dim s As String = Now.ToLongDateString + " " + Now.ToShortTimeString<br/>
<br/>
e.Graphics.DrawString(s, New Font(dgv.Font, FontStyle.Bold), _<br/>
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - _<br/>
e.Graphics.MeasureString(s, New Font(dgv.Font, FontStyle.Bold), _<br/>
e.MarginBounds.Width).Width), e.MarginBounds.Top - _<br/>
e.Graphics.MeasureString(PrintTitle, _<br/>
New Font(New Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), _<br/>
e.MarginBounds.Width).Height - 13)<br/>
<br/>
Draw Columns<br/>
tmpTop = e.MarginBounds.Y + 350<br/>
i = 0<br/>
For Each GridCol As DataGridViewColumn In dgv.Columns<br/>
If Not GridCol.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(GridCol.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
e.Graphics.FillRectangle(New SolidBrush(Drawing.Color.LightGray), _<br/>
New Rectangle(ColumnLefts(i), tmpTop, ColumnWidths(i),
HeaderHeight))<br/>
<br/>
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), HeaderHeight))<br/>
<br/>
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, _<br/>
New SolidBrush(GridCol.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i),
_<br/>
HeaderHeight), StrFormat)<br/>
i += 1<br/>
Next<br/>
NewPage = False<br/>
<br/>
tmpTop += HeaderHeight<br/>
End If<br/>
<br/>
i = 0<br/>
For Each Cel As DataGridViewCell In GridRow.Cells<br/>
If Not Cel.OwningColumn.Visible Then Continue For<br/>
If Not SelectedColumns.Contains(Cel.OwningColumn.HeaderText) Then<br/>
Continue For<br/>
End If<br/>
<br/>
For the TextBox Column<br/>
If ColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse _<br/>
ColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then<br/>
<br/>
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _<br/>
New SolidBrush(Cel.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i), tmpTop, ColumnWidths(i),
_<br/>
CellHeight), StrFormat)<br/>
<br/>
For the Button Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then<br/>
<br/>
CellButton.Text = Cel.Value.ToString<br/>
CellButton.Size = New Size(ColumnWidths(i), CellHeight)<br/>
Dim bmp As New Bitmap(CellButton.Width, CellButton.Height)<br/>
CellButton.DrawToBitmap(bmp, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
<br/>
For the CheckBox Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewCheckBoxColumn) Then<br/>
<br/>
CellCheckBox.Size = New Size(14, 14)<br/>
CellCheckBox.Checked = CType(Cel.Value, Boolean)<br/>
Dim bmp As New Bitmap(ColumnWidths(i), CellHeight)<br/>
Dim tmpGraphics As Graphics = Graphics.FromImage(bmp)<br/>
tmpGraphics.FillRectangle(Brushes.White, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
CellCheckBox.DrawToBitmap(bmp, New Rectangle(CType((bmp.Width - _<br/>
CellCheckBox.Width) / 2, Int32), CType((bmp.Height
- _<br/>
CellCheckBox.Height) / 2, Int32), CellCheckBox.Width,
_<br/>
CellCheckBox.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
<br/>
For the ComboBox Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewComboBoxColumn) Then<br/>
<br/>
CellComboBox.Size = New Size(ColumnWidths(i), CellHeight)<br/>
Dim bmp As New Bitmap(CellComboBox.Width, CellComboBox.Height)<br/>
CellComboBox.DrawToBitmap(bmp, New Rectangle(0, 0, _<br/>
bmp.Width, bmp.Height))<br/>
e.Graphics.DrawImage(bmp, New Point(ColumnLefts(i), tmpTop))<br/>
e.Graphics.DrawString(Cel.Value.ToString, Cel.InheritedStyle.Font, _<br/>
New SolidBrush(Cel.InheritedStyle.ForeColor),
_<br/>
New RectangleF(ColumnLefts(i) + 1, tmpTop, ColumnWidths(i)
_<br/>
- 16, CellHeight), StrFormatComboBox)<br/>
<br/>
For the Image Column<br/>
ElseIf ColumnTypes(i) Is GetType(DataGridViewImageColumn) Then<br/>
<br/>
Dim CelSize As Rectangle = New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), CellHeight)<br/>
Dim ImgSize As Size = CType(Cel.FormattedValue, Image).Size<br/>
e.Graphics.DrawImage(Cel.FormattedValue, New Rectangle(ColumnLefts(i) _<br/>
+ CType(((CelSize.Width - ImgSize.Width) / 2),
Int32), _<br/>
tmpTop + CType(((CelSize.Height - ImgSize.Height)
/ 2), _<br/>
Int32), CType(Cel.FormattedValue, Image).Width,
CType(Cel.FormattedValue, _<br/>
Image).Height))<br/>
<br/>
End If<br/>
<br/>
Drawing Cells Borders
<br/>
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(ColumnLefts(i), _<br/>
tmpTop, ColumnWidths(i), CellHeight))<br/>
<br/>
i += 1<br/>
<br/>
Next<br/>
tmpTop += CellHeight<br/>
<br/>
End If<br/>
<br/>
RowPos += 1<br/>
For the first page it calculates Rows per Page<br/>
If PageNo = 1 Then<br/>
RowsPerPage += 1<br/>
End If<br/>
Loop<br/>
<br/>
If RowsPerPage = 0 Then Exit Sub<br/>
<br/>
<br/>
<br/>
<br/>
DrawFooter(e, RowsPerPage)<br/>
<br/>
e.HasMorePages = False<br/>
e.Graphics.DrawString("total", Mysimple1, Brushes.Black, x + 60, y + 910)<br/>
e.Graphics.DrawString(c, Mysimple, Brushes.Black, x, y)<br/>
e.Graphics.DrawString("Discount", Mysimple1, Brushes.Black, x + 250, y + 920)<br/>
e.Graphics.DrawString(d, Mysimple, Brushes.Black, x + 360, y + 990)<br/>
e.Graphics.DrawString("Shiping Charges", Mysimple1, Brushes.Black, x + 250, y + 930)<br/>
e.Graphics.DrawString(f, Mysimple, Brushes.Black, x + 310, y + 990)<br/>
e.Graphics.DrawString("Sales Tax", Mysimple1, Brushes.Black, x + 250, y + 940)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
e.Graphics.DrawString("Sales Tax", Mysimple1, Brushes.Black, x + 250, y + 950)<br/>
e.Graphics.DrawString(g, Mysimple, Brushes.Black, x + 330, y + 310)<br/>
Catch ex As Exception<br/>
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)<br/>
Finally<br/>
<br/>
End Try<br/>
End Sub<br/>
<br/>
Private Shared Sub DrawFooter(ByVal e As System.Drawing.Printing.PrintPageEventArgs, ByVal RowsPerPage As Int32)<br/>
Dim cnt As Integer<br/>
<br/>
Detemining rows number to print<br/>
If PrintAllRows Then<br/>
If dgv.Rows(dgv.Rows.Count - 1).IsNewRow Then<br/>
When the DataGridView doesnt allow adding rows<br/>
cnt = dgv.Rows.Count - 2<br/>
Else<br/>
When the DataGridView allows adding rows<br/>
cnt = dgv.Rows.Count - 1<br/>
End If<br/>
Else<br/>
cnt = dgv.SelectedRows.Count<br/>
End If<br/>
<br/>
Writing the Page Number on the Bottom of Page<br/>
Dim PageNum As String = PageNo.ToString + " of " + _<br/>
Math.Ceiling(cnt / RowsPerPage).ToString<br/>
e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, _<br/>
e.MarginBounds.Left + (e.MarginBounds.Width - _<br/>
e.Graphics.MeasureString(PageNum, dgv.Font, _<br/>
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + _<br/>
e.MarginBounds.Height + 31)<br/>
<br/>
End Sub<br/>
<br/>
End Class
View the full article