I need to print a Win Form, that is the result of a query to a RDBMS (MS SQL Server 2005).
With "CopyFromScreen" method, I print only what I see on the video. I need to print all the result of the query of
DataTable(). What can I do? I don't find another method that copy all the grid...
Another problem are the margins: also if I see all the grid on the video, the print margins are smaller.
What can I do?
If someone needs something else to help me, ask me.
Thanks a lot for your helps!
Gabriele
This is a part of the code:
Imports System
Imports System.Data
Imports
System.Windows.Forms
Imports
System.Data.SqlClient
Imports
System.Drawing
Public
Class BindData
Inherits System.Windows.Forms.Form
Private dataGridView1 As New DataGridView()
Private bindingSource1 As New BindingSource()
Private dataAdapter As New SqlDataAdapter()
Private WithEvents reloadButton As New Button()
Private WithEvents submitButton As New Button()
Private WithEvents closeButton As New Button()
Dim memoryImage As Bitmap ' CaptureScreen(), printButton_Click(), printPreviewButton_Click()
Private WithEvents printButton As New Button
Private WithEvents printDocument1 As New System.Drawing.Printing.PrintDocument
Private WithEvents printPreviewButton As New Button
Private printPreviewDialog1 As New PrintPreviewDialog()
Public Sub New()
Me.Size = New System.Drawing.Size(900, 600)
Me.dataGridView1.Dock = DockStyle.Fill
Me.reloadButton.Text = "RELOAD"
Me.submitButton.Text = "SUBMIT"
Me.closeButton.Text = "CLOSE"
Me.printButton.Text = "PRINT"
Me.printPreviewButton.Text = "PREVIEW"
Dim panel As New FlowLayoutPanel()
panel.Dock = DockStyle.Top
panel.AutoSize =
True
panel.Controls.AddRange(
New Control() _
{
Me.reloadButton, Me.submitButton, Me.printButton, Me.printPreviewButton, Me.closeButton})
Me.Controls.AddRange(New Control() {Me.dataGridView1, panel})
Me.Text = "Query output to database"
End Sub
Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Me.dataGridView1.DataSource = Me.bindingSource1
Me.GetData()
End Sub
Protected Sub GetData()
Try
Dim connection As SqlConnection = New SqlConnection(GetConnectionString())
Dim command As SqlCommand = New SqlCommand("uspGetqueryString6", connection)
Me.dataAdapter = New SqlDataAdapter(command)
Me.dataAdapter.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Console.WriteLine(
"Number rows of the table: {0}", Me.dataAdapter.Fill(table))
Me.bindingSource1.DataSource = table 'System.Windows.Forms.BindingSource
Me.dataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.DisplayedCells)
'System.Windows.Forms.DataGridView
Catch e As System.Exception
System.Windows.Forms.MessageBox.Show("Error")
System.Console.WriteLine("e.Message(): {0}", e.Message())
End Try
End Sub
Public Sub printButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles printButton.Click
CaptureScreen()
printDocument1.Print()
End Sub
Public Sub printPreviewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles printPreviewButton.Click
CaptureScreen()
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.ShowDialog()
End Sub
Public Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
End Sub
Public Sub printDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub