Member Avatar for සශික

I had button code to print data in data grid view on vb.net program. But when I click on print all times it show print preview lost first of record.
this is my code

Dim mRow As Integer = 0
        Dim newpage As Boolean = True
        With dgvreciept
            Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
            fmt.LineAlignment = StringAlignment.Center
            fmt.Trimming = StringTrimming.EllipsisCharacter
            Dim y As Single = e.MarginBounds.Top
            Do While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = e.MarginBounds.Left
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage) Then
                        e.Graphics.DrawString(dgvreciept.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                    Else
                        e.Graphics.DrawString(dgvreciept.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)
                Next
                newpage = False
                y += h
                mRow += 1
                If y + h > e.MarginBounds.Bottom Then
                    e.HasMorePages = True
                    mRow -= 1
                    newpage = True
                    Exit Sub
                End If
            Loop
            mRow = 0
        End With

I change ' Dim mRow As Integer = 0' s value to 1 and run code. that time it lost 2nd alseo.
I change it to -1 and try. it show error.
can enyone help this ?

I think your problem is here:

If (newpage) Then
                        e.Graphics.DrawString(dgvreciept.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                    Else
                        e.Graphics.DrawString(dgvreciept.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)

On the first row to be processed (whether set to 0 or 1) newPage is true but inside that if you only draw the header if new page is true. You also need to draw the string with the row data. This is better:

If (newpage) Then
     e.Graphics.DrawString(dgvreciept.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
      // you'll need to recalculate rc of course if newPage is true
End If
// now draw string for cell regardless of newPage state
e.Graphics.DrawString(dgvreciept.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)

It's a bit concerning that you even attempted a value of -1 when trouble shooting. Any index check would immediately fail with such a value:
.Rows(mRow) // with mRow = -1, out of bounds error
but you live and you learn :)

@සශික : Nothing wrong in your codes to print data.
Just alter some lines in your codes.
1) Never declare the variable Dim mRow As Integer = 0 at Event level like PrintPage Event, always declare it at Form level and set its value to 0 at BeginPrint Event.
2) Just modify the codes at line No. 23,24 and 25 like

If newpage then
    newpage = False
Else
    mRow +=1
End If
y += h

Hope it can help you.

Member Avatar for සශික

okay, I put above code. Now 1st record appeared in new print preview. But First row and row headers print on itself.
NOTE: I load column names from the database.
error.PNG

I just tried your codes in my mechine and I have altered one to two lines you can find them in following codes.

Public Class Form1
    Dim mRow As Integer = 0
    Dim newpage As Boolean = True

    Private Sub PrintDocument1_BeginPrint(sender As Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        mRow = 0
        newpage = True
    End Sub

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

        With dgvreciept
            Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
            fmt.LineAlignment = StringAlignment.Center
            fmt.Trimming = StringTrimming.EllipsisCharacter
            Dim y As Single = e.MarginBounds.Top
            Do While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = e.MarginBounds.Left
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage) Then
                        e.Graphics.DrawString(dgvreciept.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
                    Else
                        e.Graphics.DrawString(dgvreciept.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)
                Next
                If newpage Then
                    newpage = False
                Else
                    mRow += 1
                End If
                y += h
                If y + h > e.MarginBounds.Bottom Then
                    e.HasMorePages = True
                    'mRow -= 1
                    newpage = True
                    Exit Sub
                End If
            Loop
            mRow = 0
        End With
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.Print()
        PrintPreviewDialog1.Show()

    End Sub
End Class

Hope it can help you. Executing it I got perfect result

Member Avatar for සශික

aha :) thank you broH

Dear sir, i got very helpful this knowladge, also can you help me how to add header & fotter in Print, awaiting for favorable reply, thanks in advanced

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.