Printing DatagridView

Hi,
I'd like to print only certain columns of a DataGridView. How would something like this be done? I couldn't find any code examples.
Thanks
[160 byte] By [TomFrey] at [2007-12-16]
# 1
There isn't any built in printing support, so there are two options:
1) Use the standard printing system provided by Windows Forms and manually print out the information that you want. Check out the documentation for PrintPage and PrintDocument

2) Use the DataGridView's clipboard copy support to copy the content to an Excel file and then print that.

Hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
ok, I just wrote a class that works with PrintPage and PrintDocument and everything looks good in the print preview dialog. However, as soon as I'm printing, only the grid headers and some extra text I added (page numbers, etc.) are printed. None of the DataGridView content that I painted and is visible in the print preview is printed to the output document.
Any idea why this could be the case?
TomFrey at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Wow, I haven't see that, unless you are using TextRenderer.DrawText for your printing. Unfortunitely TextRenderer.DrawText uses GDI while Graphics.DrawString uses GDI+. GDI drawing with our current print engine doesn't work correctly, so you'll need to use Graphics.DrawString.

Let me know if that works. If that isn't the case then can you file a bug and we'll investigate it? Thanks

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
I'm using DrawString...
here's my code ... it's a bit messy right now but should do the job ;)

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int x = 0;
int y = 100;
int rowGap = 20;
int colGap = 5;
int leftMargin = 50;

int linesPerPage = e.MarginBounds.Height / _dataGridView.DefaultCellStyle.Font.Height;
Font font = new Font(_dataGridView.DefaultCellStyle.Font.FontFamily, _dataGridView.DefaultCellStyle.Font.Size);
Font headingFont = new Font("Arial", 10, FontStyle.Bold);
Font captionFont = new Font("Arial", 10, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
string cellValue = "";
e.Graphics.DrawString("Erstellt von " + ObjectStore.Account.UserName + " am " + DateTime.Now.ToLongDateString(), captionFont, brush, new PointF(50, 50));
e.Graphics.DrawString("Seite " + _pageNumber, captionFont, brush, new PointF(e.PageBounds.Width - 100, e.PageBounds.Height - 50));
int rowCount = _dataGridView.Rows.Count;
int colCount = _dataGridView.Columns.Count;
y += rowGap;
x = leftMargin;
foreach (DataGridViewColumn column in _dataGridView.Columns)
{
if(column.GetType() != typeof(DataGridViewButtonColumn) && column.GetType() != typeof(DataGridViewCheckBoxColumn))
{
cellValue = column.HeaderText;
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += column.Width + colGap;
}
}
int count = 0;
for (int i = _rowPosition; i < _dataGridView.Rows.Count; i++)
{
y += rowGap;
x = leftMargin;
foreach (DataGridViewColumn column in _dataGridView.Columns)
{
if (column.GetType() != typeof(DataGridViewButtonColumn) && column.GetType() != typeof(DataGridViewCheckBoxColumn))
{
cellValue = _dataGridView.Rows//emoticons/emotion-55.gif" alt="Idea" />.Cells[column.Index].Value.ToString();
e.Graphics.DrawString(cellValue, captionFont, brush, x, y);
x += column.Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] { '\r', '\n' }).Length - 1);
}
}
_rowPosition++;
count++;
if (count > linesPerPage)
{
break;
}
}
if (_rowPosition > linesPerPage && _rowPosition < _dataGridView.Rows.Count)
{
_pageNumber++;
e.HasMorePages = true;
}
}


TomFrey at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
any takers? ;)
TomFrey at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
The MSDN library provides the following guidance for printing a DataGrid...

This example demonstrates printing a DataGrid control.

Example

private void printGrid_Click(System.Object sender, System.EventArgs e) { printDocument1.Print(); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size)); this.InvokePaint(dataGrid1, myPaintArgs); 
Is it planned to have this work for the DataGridView?
I have tried to use it but it printed only the Grid, omitting the contents of the cells.
Does the code in the above example work in VS 2005? That is, maybe I should copy
the contents of my DataGridView to a DataGrid and print from there!
GraemeWT at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

For anyone needing to know how to start printing a grid, here is some code I whipped up after not finding any samples I could heist from someone else. I'm sure there are better ways to do this, but like I said, this is a start. Probably why MS doesn't offer a print function or component is because there is truly an unlimited number of configurations for a print setup. To achieve most of them isn't even rocket science, the beauty of VB.NET. Cheers.


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 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 DataGridView1.Columns

nTotalWidth += oColumn.Width

Next
nPageNo = 1
NewPage = True
nRowPos = 0

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As 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 DataGridView1.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 < DataGridView1.Rows.Count - 1

Dim oRow As DataGridViewRow = DataGridView1.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(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.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(DataGridView1.Rows.Count / RowsPerPage).ToString

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

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

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

End Sub

will_affinity at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

Will,

This is great code. Thank you!!! This works great for a straight print. I've played around with the code but can't seem to get it to work with print preview or a printdialog control. Do you have any code or have any idea on how to modofy your code so that it works with either control?

Thanks,

VB-Bandit

bebandit at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9

VB-Bandit,

The code above creates the PrintDocument now all you need to do is use it, the following code with give you a print preview!

Dim dlg As New PrintPreviewDialog()

dlg.Document = PrintDocument1

dlg.ShowDialog()

This code gives you the page setup form...

Dim psDlg As New PageSetupDialog

Dim storedPageSettings As PageSettings = New PageSettings()

psDlg.PageSettings = storedPageSettings

psDlg.ShowDialog()

I hope this is what you were looking for!

CoNNect

CoNNect at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10

Thanks Heaps for your code Will!!!

BTW, your code drops off the last line, it's fixed now as below:

CoNNect at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11
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 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 DataGridView1.Columns

nTotalWidth += oColumn.Width

Next
nPageNo = 1
NewPage = True
nRowPos = 0

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As 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 DataGridView1.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 < DataGridView1.Rows.Count - 1

Dim oRow As DataGridViewRow = DataGridView1.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(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

' Draw Columns
nTop = e.MarginBounds.Top
i = 0
For Each oColumn As DataGridViewColumn In DataGridView1.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(DataGridView1.Rows.Count / RowsPerPage).ToString

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

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

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

End Sub

CoNNect at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 12
Hy guys.

I must say that this is a great code. But i get following error
Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Problem is with Handles PrintDocument1.PrintPage

Can you please tell me what i'm doing wrong?

Regards...

Nectuss at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 13
Ok i managed to solve that. But now i got one serious problem! This code print all rows from my DataGridView except last one. It always show one page more that actually exist.

I have try your code too CoNNect but there is same problem with last row...

Some help will be very wellcome!

Nectuss at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 14
Ok i have managed that too. If anyone will run into similar problem here is a way to set it right.

In while loop in posted code you must set Do While nRowPos < DataGridView1.Rows.Count - 1 you must delet -1. It should look like this Do While nRowPos < DataGridView1.Rows.Count. Then it will work just fine.

Great code indeed. Thank you...

Nectuss at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...