i had manage to print the contents of my datagrid view, but it doesnt fit in portrait mode. i guess printing it in landscape mode will do.
i have this code for my dataset to fill the datagridview.
Private Sub print_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim query As String = "SELECT * FROM TBLusers"
Dim DA As New SqlDataAdapter(query, CN)
Dim DS As New DataSet
CN.Open()
DA.Fill(DS, "Users")
CN.Close()
DataGridView1.DataSource = DS.Tables("Users")
'DataGridView1.DataMember = "Users"
End Sub
here is the function for printing i guess? i got it from a tutorial.
Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim dataGridViewImage As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
DataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
e.Graphics.DrawImage(dataGridViewImage, 0, 0)
End Sub
here is the print preview.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PrintPreviewDialog2.Document = PrintDocument1
PrintPreviewDialog2.PrintPreviewControl.Zoom = 1
PrintPreviewDialog2.ShowDialog()
End Sub
and the print...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
i tried
PrintDocument1.PrinterSettings.DefaultPageSettings.Landscape = True
but it doesnt seems to work?
Aryan Kumar
06-Jul-2023Sure, I can help you with that. Here are the steps on how to print in landscape mode in VB.NET:
System.Drawing
namespace.PrintDocument
object.Landscape
property of the PageSettings object toTrue
.Here is an example of how to print in landscape mode in VB.NET:
VB.Net
This code will create a new PrintDocument object and set the PageSettings object to landscape mode. The code will then print the document.
Here is an explanation of the code:
PrintDocument
class represents a PrintDocument object.PageSettings
class represents the page settings for a PrintDocument object.Landscape
property of thePageSettings
class specifies whether the document should be printed in landscape mode.Print
method of thePrintDocument
class prints the document.Anonymous User
05-Oct-2013You're setting the DefaultPage setting for the printer. Try to set it for the document
itself: