I have vb.net application.
I have following code to print dgv.
but when i print, all the page get printed.
How can I print selected pages to be printed with pagesetup.

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

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PageSetup As PageSetupDialog
        PageSetup = New PageSetupDialog
        PageSetup.PageSettings = PrintDocument1.DefaultPageSettings
        PageSetupDialog1.ShowDialog()
        PrintDocument1.Print()

    End Sub


Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim custCells As Integer() = {1, 3, 4}
        With PatientDetailsDataGridView
            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 Integer In custCells
                    Dim rc As RectangleF = New RectangleF(x, y, row.Cells(cell).Size.Width, row.Cells(cell).Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage) Then
                        e.Graphics.DrawString(PatientDetailsDataGridView.Columns(cell).HeaderText, .Font, Brushes.Black, rc, fmt)
                    Else
                        e.Graphics.DrawString(PatientDetailsDataGridView.Rows(row.Cells(cell).RowIndex).Cells(cell).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
    end sub
End Class

Recommended Answers

All 12 Replies

I went through the url provided by Mr. M but it shows - only selected (by default) page no. to print.

In my DGV there are new pages being added day to day.

So I want only selected page (not fixed everytime) to be printed.

So how can I do?

That's simple. You don't want to print all pages right? Your DGV is updated with new pages everyday right, now you have a code to print certain pages all that's left now was for you to add some sort of a tool and use that tool as identifier of a page to print. I don't know how your page are placed (are they in order or) but the idea of what you can do now is:

1) Add let's say a ListBox and populate the number of pages there. (Don't populate the total, you need to populate "1,2,3,-to the last page") you can use the "For Each" to get the numbers. Or

2) Add a TextBox and on the code on the link I posted above change the hard coded number to TextBox and on a TextBox you will only type the number and click print that will just print the page number you choose.

Here is it suppose you go with the second option:

 Application.PrintOut
 FileName:="", Copies:=1,
 Range:=wdPrintRangeOfPages,
 Pages:=TextBox1.Text

Also if you would like to let a user choose how many copies you will have to add another tool for it.

Where should I put the above code??

www.vbdotnetforums.com/reporting-printing/7947-printing-range.html

Sorry not able to load that downloaded application...

What's the error?

The link u provided shows to download a ZIP file..

I downloaded and opened it but shows nothing.

No form, no code. only windows application

i found this error.

by the way finally i downloaded the code but again it prints all the pages, not the selected page....

What I think you will need to do is add 2 textbox, one for "FromPage" and another for "ToPage" properties which indicates which pages to be printed, its uses a Range, say you want to print from page 3 to 7 so on the first textbox you will write 3 and on the other textbox you will write 7.

 ' Example
 PrintDialog1.PrinterSettings.FromPage.Equals(TextBox1.Text)
 PrintDialog1.PrinterSettings.ToPage.Equals(TextBox2.text)

I think modifying like that will enable you to print only that page range.

Sorry not able to solve problem,
Again print all pages.
Would you please modify my above first code...

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.