0

I am running into an issue where I am attempting to add some blank rows to a datagridview before printing the form for use.

I am having an issue trying to understand where I need to put the code to add the rows to the data gridview so they are added and captured before the user hits print. My concern is that I am not adding the rows in the right spot before the screen is captured however the code to add the rows is run before the code to hide the buttons is implemented however the buttons are removed and the rows are not added in the actual printed image.

If that is not clear hopefully these images will make more sense. At this point I am not sure why the buttons being hidden are being captured but the added rows are not when based on the code it looks like the rows are being added before the buttons are hidden.

Thank you.

Here is the Image before the user hits print Before Print

Here is the Image after the user hits Print During Print

And here is what actually printed Actual Printed

And here is the code in question that captures the screen

    Private Sub CaptureScreen()

    Dim myGraphics As Graphics = Me.CreateGraphics()
    Dim s As Size = Me.Size

    If dgvReceive.Rows.Count < 27 Then
        Dim rowstoadd As Integer = 0
        rowstoadd = 27 - dgvReceive.Rows.Count
        dgvReceive.Rows.Add(rowstoadd)
    End If

    If MemoryImage IsNot Nothing Then
        MemoryImage.Dispose()
    End If

    MemoryImage = New Bitmap(s.Width, s.Height, myGraphics)

    For Each btn As Button In Me.Controls.OfType(Of Button)
        btn.Visible = False
    Next
    dgvReceive.ClearSelection()
    lblTitle.Select()

    Me.FormBorderStyle = FormBorderStyle.None

    Dim memoryGraphics As Graphics = Graphics.FromImage(MemoryImage)
    memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)

    Me.FormBorderStyle = FormBorderStyle.Sizable
    For Each btn As Button In Me.Controls.OfType(Of Button)
        btn.Visible = True
    Next
End Sub
Tom
  • 191
  • 1
  • 15
  • 2
    The added rows won't be visible until the grid repaints itself. Which won't happen until after this code completes. Insert `dgvReceive.Update()` to force that to happen earlier. This isn't the right way to do it, but at least you can get ahead. – Hans Passant Jan 15 '20 at 15:22
  • @HansPassant thank you. If it isnt too much trouble would you be able to point me to the right way? – Tom Jan 15 '20 at 15:26
  • [Create a Tiff Bitmap file from a DatagridView](https://stackoverflow.com/a/59633044/7444103). Of course, you can choose another Image format, like `PNG`, if you need to save the Image (avoid `JPEG`, this compression format is not appropriate for this kind of Bitmaps). – Jimi Jan 15 '20 at 16:59

0 Answers0