title: VB .Net Print Datagrid in Multiple Pages
body: <p>I’m new to VB .Net and this website has helped me a lot. I want to print the data from my datagrid and it doesn’t print properly.</p>
  
  <p>With this code, it will print 1 page only and prints what fits in one page. The rest that doesn’t fit first page will not be printed.</p>
  
  <pre><code>If gridPTrans.RowCount &gt; 0 Then 'Datagrid is not empty
      y = y + 25 'Height
      With gridPTrans
          Do While mRow &lt; .RowCount 'Loop datagrid
              If mRow = .RowCount - 1 Then
                  Exit Sub
              End If
  
              row = .Rows(mRow)
              drawPoint = New PointF(x, y)
  
              For Each cell As DataGridViewCell In row.Cells
                  MessageBox.Show("Y: " &amp; y)
                  strValue = (intValue.ToString + ".  " + gridPTrans.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString())
                  e.Graphics.DrawString(strValue, drawFont, drawBrush, drawPoint)
                  intValue += 1
                  MessageBox.Show(" " &amp; strValue)
              Next
              y += 18
              mRow += 1
          Loop 
          mRow = 0
      End With 
  End If 
  
  If y &gt; e.MarginBounds.Bottom Then
      e.HasMorePages = True
      MessageBox.Show("e.HasMorePages: " &amp; e.HasMorePages)
      y = (e.MarginBounds.Top) / 2
      Exit Sub
  End If
  </code></pre>
  
  <p>I tried a different code. With this code, it will print infinite pages of the first page.</p>
  
  <pre><code>If gridPTrans.RowCount &gt; 0 Then 'TRANSFER is not empty
      y = y + 25
      With gridPTrans
          Do While mRow &lt; .RowCount 'Loop datagrid
              If mRow = .RowCount - 1 Then
                  Exit Sub
              End If
  
              row = .Rows(mRow)
              drawPoint = New PointF(x, y)
  
              If y &gt; e.MarginBounds.Bottom Then
                  e.HasMorePages = True
                  MessageBox.Show("e.HasMorePages: " &amp; e.HasMorePages)
                  y = (e.MarginBounds.Top) / 2
                  Exit Sub
              End If
  
              'Print datagrid
              For Each cell As DataGridViewCell In row.Cells
                  MessageBox.Show("Y: " &amp; y)
                  strValue = (intValue.ToString + ".  " + gridPTrans.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString())
                  e.Graphics.DrawString(strValue, drawFont, drawBrush, drawPoint)
                  intValue += 1
                  MessageBox.Show(" " &amp; strValue)
              Next
              y += 18
              mRow += 1
          Loop
          mRow = 0    
      End With 
  End If 
  </code></pre>