I'm working on a program where I have two DataGridViews in a Windows form. When I select a row in the primary DataGridView, corresponding data appears in the secondary DataGridView. Now I would like to print this data such that data on page 1 is from the primary DataGridView and from page 2 data from the secondary DataGridView that can contain several hundred rows or nothing.
But how is this done?

If I make a loop inside the PrintDocument with data from the secondary DataGridview, I only get pages of data from both DataGridViews printed on top of each other. The code to print the primary DataGridView works perfectly, but I need to get data from the secondary DataGridView on page 2.
Some tips or links to something that shows me how to do it?

VB Code:
  1. Try
  2.             Dim margin As New Printing.Margins(25, 15, 15, 15)
  3.             PrintDoc.DefaultPageSettings.Margins = margin
  4.  
  5.             'Set Units
  6.             Dim mm As Graphics = e.Graphics
  7.             mm.PageUnit = GraphicsUnit.Millimeter
  8.             'Fonts
  9.             Dim HeaderFont As Font = New Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Point)
  10.             Dim infoFont As Font = New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)
  11.             Dim FooterFont As Font = New Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Point)
  12.             Dim BodyFont As Font = New Font("Times New Roman", 11, FontStyle.Regular, GraphicsUnit.Point)
  13.             'StringFormat to align Beskrivelse
  14.             Dim stfBeskriv As New StringFormat
  15.             stfBeskriv.Alignment = StringAlignment.Near
  16.             stfBeskriv.LineAlignment = StringAlignment.Near
  17.             'Create a rectangel to hold Beskrivelse
  18.             Dim rectBeskriv As New Rectangle(25, 68, 165, 144) 'L, T, W, H
  19.             'StringFormat to align Klausuler
  20.             Dim stfKlausuler As New StringFormat
  21.             stfKlausuler.Alignment = StringAlignment.Near
  22.             stfKlausuler.LineAlignment = StringAlignment.Near
  23.             'Create a rectangel to hold Klausuler
  24.             Dim rectKlausuler As New Rectangle(25, 233, 165, 30)
  25.             'Lines
  26.             Dim blackPen As New Pen(Color.Black, 0.25) 'Thin line
  27.             'AntiAlias
  28.             e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
  29.  
  30.             Dim i As Integer = Me.dgvAfond.CurrentRow.Index
  31.  
  32.             'Blanket and ARegNr contracted to A-XXXX - one line
  33.             '2 Blanket
  34.             Dim strBlanket, strARegNr As String
  35.             If IsDBNull(Me.dgvAfond.Item(2, i).Value) Then
  36.                 strBlanket = "A"
  37.             Else
  38.                 strBlanket = CStr(Me.dgvAfond.Item(2, i).Value)
  39.             End If
  40.             '3 ARegNr
  41.             If IsDBNull(Me.dgvAfond.Item(3, i).Value) Then
  42.                 strARegNr = "0000"
  43.             Else
  44.                 strARegNr = CStr(Me.dgvAfond.Item(3, i).Value)
  45.             End If
  46.  
  47.             e.Graphics.DrawString("Afond nr.: " & strBlanket & "-" & strARegNr, HeaderFont, Brushes.Black, 25, 15)
  48.  
  49.             '18 SærNr - one line
  50.             If IsDBNull(Me.dgvAfond.Item(18, i).Value) Then
  51.                 e.Graphics.DrawString("Særnr.: " & "", HeaderFont, Brushes.Black, 25, 20)
  52.             Else
  53.                 e.Graphics.DrawString("Særnr.: " & CStr(Me.dgvAfond.Item(18, i).Value), HeaderFont, Brushes.Black, 25, 20)
  54.             End If
  55.             '6 JournalNr - one line
  56.             If IsDBNull(Me.dgvAfond.Item(6, i).Value) Then
  57.                 e.Graphics.DrawString("Indkomstjournal: " & "", HeaderFont, Brushes.Black, 25, 25)
  58.             Else
  59.                 e.Graphics.DrawString("Indkomstjournal: " & CStr(Me.dgvAfond.Item(6, i).Value), HeaderFont, Brushes.Black, 25, 25)
  60.             End If
  61.  
  62.             'RVM Logo - upper right corner
  63.             e.Graphics.DrawImage(My.Resources.RMA, 130, 15)
  64.  
  65.             'Dato and Signatur - one line
  66.             '23 Dato
  67.             If IsDBNull(Me.dgvAfond.Item(23, i).Value) Then
  68.                 e.Graphics.DrawString("Reg. Dato: " & "", HeaderFont, Brushes.Black, 25, 32)
  69.             Else
  70.                 e.Graphics.DrawString("Reg. Dato: " & CStr(Me.dgvAfond.Item(23, i).Value), HeaderFont, Brushes.Black, 25, 32)
  71.             End If
  72.             '24 Registrant
  73.             If IsDBNull(Me.dgvAfond.Item(24, i).Value) Then
  74.                 e.Graphics.DrawString("Registrant: " & "", HeaderFont, Brushes.Black, 70, 32)
  75.             Else
  76.                 e.Graphics.DrawString("Registrant: " & CStr(Me.dgvAfond.Item(24, i).Value), HeaderFont, Brushes.Black, 70, 32)
  77.             End If
  78.  
  79.             'Horizontal Line
  80.             e.Graphics.DrawLine(blackPen, 25, 38, 103, 38)
  81.  
  82.             'Titel - two lines
  83.             '4 Title header
  84.             e.Graphics.DrawString("Titel: ", HeaderFont, Brushes.Black, 25, 40)
  85.             'Title text
  86.             If IsDBNull(Me.dgvAfond.Item(4, i).Value) Then
  87.                 e.Graphics.DrawString("", HeaderFont, Brushes.Black, 25, 45)
  88.             Else
  89.                 e.Graphics.DrawString(CStr(Me.dgvAfond.Item(4, i).Value), HeaderFont, Brushes.Black, 25, 45)
  90.             End If
  91.  
  92.             '9 Sted - one line
  93.             If IsDBNull(Me.dgvAfond.Item(9, i).Value) Then
  94.                 e.Graphics.DrawString("Sted: " & "", infoFont, Brushes.Black, 25, 53)
  95.             Else
  96.                 e.Graphics.DrawString("Sted: " & CStr(Me.dgvAfond.Item(9, i).Value), infoFont, Brushes.Black, 25, 53)
  97.             End If
  98.  
  99.             'Horizontal Line
  100.             e.Graphics.DrawLine(blackPen, 25, 58, 190, 58)
  101.  
  102.             'Beskrivelse - two or more lines
  103.             'Beskrivelse header
  104.             e.Graphics.DrawString("Beskrivelse: ", HeaderFont, Brushes.Black, 25, 61)
  105.             '7 Beskrivelse text
  106.             If IsDBNull(Me.dgvAfond.Item(7, i).Value) Then
  107.                 e.Graphics.DrawString("", BodyFont, Brushes.Black, rectBeskriv, stfBeskriv)
  108.             Else
  109.                 e.Graphics.DrawString(CStr(Me.dgvAfond.Item(7, i).Value), BodyFont, Brushes.Black, rectBeskriv, stfBeskriv)
  110.             End If
  111.  
  112.             'Horizontal Line
  113.             e.Graphics.DrawLine(blackPen, 25, 215, 190, 215)
  114.  
  115.             'YderÅr, Stiftet og Nedlagt - one line
  116.             '10 Stiftet
  117.             If IsDBNull(Me.dgvAfond.Item(10, i).Value) Then
  118.                 e.Graphics.DrawString("Født/Stiftet: " & "", infoFont, Brushes.Black, 25, 219)
  119.             Else
  120.                 e.Graphics.DrawString("Født/Stiftet: " & CStr(Me.dgvAfond.Item(10, i).Value), infoFont, Brushes.Black, 25, 219)
  121.             End If
  122.             '11 Nedlagt
  123.             If IsDBNull(Me.dgvAfond.Item(11, i).Value) Then
  124.                 e.Graphics.DrawString("Død/Nedlagt: " & "", infoFont, Brushes.Black, 70, 219)
  125.             Else
  126.                 e.Graphics.DrawString("Død/Nedlagt: " & CStr(Me.dgvAfond.Item(11, i).Value), infoFont, Brushes.Black, 70, 219)
  127.             End If
  128.             '12 YderÅr
  129.             If IsDBNull(Me.dgvAfond.Item(12, i).Value) Then
  130.                 e.Graphics.DrawString("Yderår: " & "", infoFont, Brushes.Black, 115, 219)
  131.             Else
  132.                 e.Graphics.DrawString("Yderår: " & CStr(Me.dgvAfond.Item(12, i).Value), infoFont, Brushes.Black, 115, 219)
  133.             End If
  134.  
  135.             'Horizontal Line
  136.             e.Graphics.DrawLine(blackPen, 25, 225, 190, 225)
  137.  
  138.             'Klausuler - two lines
  139.             'Klausuler header
  140.             e.Graphics.DrawString("Klausuler: ", infoFont, Brushes.Black, 25, 227)
  141.             '14 Klausuler text - one or more lines
  142.             If IsDBNull(Me.dgvAfond.Item(14, i).Value) Then
  143.                 e.Graphics.DrawString("", BodyFont, Brushes.Black, rectKlausuler, stfKlausuler)
  144.             Else
  145.                 e.Graphics.DrawString(CStr(Me.dgvAfond.Item(14, i).Value), BodyFont, Brushes.Black, rectKlausuler, stfKlausuler)
  146.             End If
  147.  
  148.             'Horizontal Line
  149.             e.Graphics.DrawLine(blackPen, 25, 256, 190, 256)
  150.  
  151.             'Pakket bind, pakker, læg og hyldemeter - one line
  152.             '15 Pakket Læg
  153.             If IsDBNull(Me.dgvAfond.Item(15, i).Value) Then
  154.                 e.Graphics.DrawString("Pakket Pakker: " & "0", infoFont, Brushes.Black, 115, 260)
  155.             Else
  156.                 e.Graphics.DrawString("Pakket Pakker: " & CStr(Me.dgvAfond.Item(15, i).Value), infoFont, Brushes.Black, 115, 260)
  157.             End If
  158.             '16 Pakket Bind
  159.             If IsDBNull(Me.dgvAfond.Item(16, i).Value) Then
  160.                 e.Graphics.DrawString("Pakket Læg: " & "0", infoFont, Brushes.Black, 25, 260)
  161.             Else
  162.                 e.Graphics.DrawString("Pakket Læg: " & CStr(Me.dgvAfond.Item(16, i).Value), infoFont, Brushes.Black, 25, 260)
  163.             End If
  164.             '17 Pakket Pakker
  165.             If IsDBNull(Me.dgvAfond.Item(17, i).Value) Then
  166.                 e.Graphics.DrawString("Pakket Bind " & "0", infoFont, Brushes.Black, 70, 260)
  167.             Else
  168.                 e.Graphics.DrawString("Pakket Bind: " & CStr(Me.dgvAfond.Item(17, i).Value), infoFont, Brushes.Black, 70, 260)
  169.             End If
  170.             '22 Hyldemeter
  171.             If IsDBNull(Me.dgvAfond.Item(22, i).Value) Then
  172.                 e.Graphics.DrawString("Hyldemeter: " & "0", infoFont, Brushes.Black, 160, 260)
  173.             Else
  174.                 e.Graphics.DrawString("Hyldemeter: " & CStr(Me.dgvAfond.Item(22, i).Value), infoFont, Brushes.Black, 160, 260)
  175.             End If
  176.  
  177.             'Horizontal Line
  178.             e.Graphics.DrawLine(blackPen, 25, 266, 190, 266)
  179.  
  180.             '13 Offentlig
  181.             Dim strOffentlig As String
  182.             If CStr(Me.dgvAfond.Item(13, i).Value) = "JA" Then
  183.                 strOffentlig = "Ja"
  184.             Else
  185.                 strOffentlig = "Nej"
  186.             End If
  187.             e.Graphics.DrawString("Tilgængelig: " & strOffentlig, infoFont, Brushes.Black, 25, 268)
  188.             'Horizontal Line
  189.             e.Graphics.DrawLine(blackPen, 25, 274, 190, 274)
  190.  
  191.             'Udskriftsdato og udskrevet af - one line at bottom of the page
  192.             e.Graphics.DrawString("Udskriftsdato: " & getDate().ToShortDateString, FooterFont, Brushes.Black, 25, 276)
  193.             e.Graphics.DrawString("Udskrevet af: " & pStrSignatur.ToUpper, FooterFont, Brushes.Black, 160, 276)
  194.  
  195.         Catch ex As Exception
  196.             Cursor = Cursors.Default
  197.             'Show Exception and exit sub
  198.             MessageBox.Show("Der opstod en undtagelse ved skabelsen af Printet af den valgte Afond." & vbCrLf &
  199.                                                 "Undtagelsen er: " & ReadException(ex), "Undtagelse Print Afond",
  200.                                                 MessageBoxButtons.OK,
  201.                                                 MessageBoxIcon.Error)
  202.             Exit Sub
  203.         End Try