Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Form1: is my DataGridView with 48 Items/Rows
Form2: is my PrintPreviewControl with "Dock: Fill"
Now I need help to separate the Rows in
DataGridView
and add for example in Page1 in my Form2, 24 Rows from the DGV and in Page2 the rest 24 Rows.
I don't know how to separate rows and import in 2 pages.
When I import the rows the way they are.. the rows go down the page and doesn't create a new page (but still even with the new page, don't know how to separate the half rows or specific value I choose into 2 pages)
Form1: Button1:
Form2.show()
Form2:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Integer = 170
Dim y As Integer = 360
Dim xwidth As Integer = 190
Dim yheight As Integer = 20
Dim fon As New Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold)
Dim rect As New Rectangle(x, 100, xwidth, yheight)
Dim rek1 As New Rectangle(40, 370, 750, 380)
e.Graphics.DrawRectangle(Pens.Black, rek1)
Dim row = Form1.DataGridView1.CurrentCell.RowIndex
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column1.Name, row).Value.ToString, fon, Brushes.Black, 60, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column2.Name, row).Value.ToString, fon, Brushes.Black, 200, 380)
e.Graphics.DrawString(Form1.DataGridView1.Item(Form1.Column3.Name, row).Value.ToString, fon, Brushes.Black, 400, 380)
End Sub
Here is an example of how you might print 24 records to a page from a DataGridView
:
Private Const RECORDS_PER_PAGE As Integer = 24
Private lastRecordIndex As Integer
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Reset the record index.
lastRecordIndex = -1
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Start printing at the next record or, if no records have been printed, the first record.
Dim firstRecordIndex = lastRecordIndex + 1
'The data entry row is not to be printed.
Dim printableRowCount = DataGridView1.Rows.Count - 1
'Print a page of data or the rest of the data, whichever is smaller.
lastRecordIndex = Math.Min(lastRecordIndex + RECORDS_PER_PAGE, printableRowCount - 1)
For i = firstRecordIndex To lastRecordIndex
Dim row = DataGridView1.Rows(i)
'Print row as appropriate.
'Print another page if and only if there is more data.
e.HasMorePages = (lastRecordIndex < printableRowCount - 1)
End Sub
The BeginPrint
event allows you to initialise any state that relates to the entire print run. The PrintPage
event allows you to print one page of data. It's up to you to track what page you're up to. The e.HasMorePages
property indicates whether there are more pages to print, i.e. whether the PrintPage
event will be raised again.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.