הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, February 14, 2009 7:05 AM
Hi, I have now managed to add and delete the database table but I would like to be able to print it. May I know how? Thanks!
All replies (30)
Sunday, February 15, 2009 7:14 AM ✅Answered | 1 vote
If print means that you want to retrieve data from database table then use following code
Dim sqlConn As New sqlconnection("Connection String") |
Dim dap As New SqlDataAdapter("SELECT * FROM [TableName]", sqlConn) |
Dim ds As New DataSet |
dap.Fill(ds) |
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1 |
MsgBox(ds.Tables(0).Rows(i).Item("ColumnName") |
Next |
You can also use DataReader instead of Dataset in above code
http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspxGaurav Khanna
Saturday, February 21, 2009 8:05 AM ✅Answered | 1 vote
Hi wnd,
You're welcome.
I use the DataGridViewPrinter class to create a sample application successfully, which can print a DataGridView.
Martin Xie - MSFT said:
2. The DataGridViewPrinter Class
http://www.codeproject.com/KB/printing/datagridviewprinter.aspx
Walkthrough:
- Download the class (DataGridViewPrinter.cs) file - 4.2 Kb
2. Open the DataGridViewPrinter.cs file, add "Public" modifier to the class like this:
public class DataGridViewPrinter
so that this library can be accessed in external assemblies.
3. Compile the DataGridViewPrinter.cs file into a dll in command line:
csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs - Add reference the dll into your project
Project menu -> Add Reference -> Browser -> Locate C:/DataGridViewPrinter.dll and add it - Code sample:
Prerequisites: Drag&drop DataGridView1, PrintDocument1, Button1 and Button2 onto Form1.
Imports System.Data.SqlClient
Imports System.Drawing.Printing
Public Class Form1
'Create DataGridViewPrinter type variable
Dim MyDataGridViewPrinter As DataGridViewPrinter
' Retrieve data from database and display on DataGridView.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As SqlConnection = New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=|DataDirectory|\SqlDatabase.mdf")
Dim cmd As SqlCommand = New SqlCommand("SELECT ID, FirstName, LastName, Address FROM CustomerTable", con)
con.Open()
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
'The Print button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SetupThePrinting() Then
PrintDocument1.Print()
End If
End Sub
'The PrintPage action for the PrintDocument control
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
If more = True Then
e.HasMorePages = True
End If
End Sub
'The PrintView button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SetupThePrinting() Then
Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
MyPrintPreviewDialog.Document = PrintDocument1
MyPrintPreviewDialog.ShowDialog()
End If
End Sub
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As PrintDialog = New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = False
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
If MyPrintDialog.ShowDialog() <> DialogResult.OK Then
Return False
End If
PrintDocument1.DocumentName = "Customers Report"
PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
PrintDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
PrintDocument1.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)
If MessageBox.Show("Do you want the report to be centered on the page", "InvoiceManager - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, True, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Else
MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
End If
Return True
End Function
End Class
If you feel interested, please leave your email, I will send the DataGridViewPrinter.dll and my sample project to you for checking.
v-maxie@microsoft.com
Sunday, February 15, 2009 3:04 AM
Bump!
I am sorry guys. I am an impatient person :(
Sunday, February 15, 2009 1:16 PM
No, say a table consists of Customer IDs, FirstName, LastName, Address. I would like to print the table by clicking on the "print" button. Thanks!
Friday, February 20, 2009 7:44 AM
wnd4459 said:
No, say a table consists of Customer IDs, FirstName, LastName, Address. I would like to print the table by clicking on the "print" button. Thanks!
Hi wnd,
Welcome to MSDN forums!
Here is one idea:
- Retrieve data from database and display on DataGridView.
2) Use print components to print DataGridView's content.
Imports System.Data.SqlClient
Public Class Form1
' Part 1: Retrieve data from database and display on DataGridView.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As SqlConnection = New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=|DataDirectory|\SqlDatabase.mdf")
Dim cmd As SqlCommand = New SqlCommand("SELECT ID, FirstName, LastName, Address FROM CustomerTable", con)
con.Open()
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
'Part 2: Use print components to print DataGridView's content.
Private Sub Print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click
' Do something
End Sub
End Class
Here are two related components to printing DataGridView for you to use in VB.NET projects.
- This library provide DataGridView printing features on the FrameWork .NET 3.5. (C#, VB.NET)
http://www.codeproject.com/KB/grid/GridDrawer.Net.aspx
- The DataGridViewPrinter Class
http://www.codeproject.com/KB/printing/datagridviewprinter.aspx
Note:
- You can download the above class libraries source code and demo projects and study them.
- You can compile the class libraries source file (.cs) into a dll, and then add reference the dll to your VB.NET project, then you can call methods of the dll.
compile in command line:
csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs
Best regards,
Martin Xie
Friday, February 20, 2009 3:42 PM
I love you so much, wo ai ni!!!! that's what I have been looking for, printing the datagridview and a report as well. duo xie la hehehe, cheers! I will try it out and let ya know whether it works or not :D
EDIT: I am sorry, may I know where should I extract the DataGridViewPrinter.cs to? Thanks!
Double edit: I tried adding the DataGridViewPrint source class by referring to http://support.microsoft.com/default.aspx/kb/811401/EN-US/ but I got many errors.
Saturday, February 21, 2009 3:56 PM
Hi Martin Xie,
Thank you for being so helpful.
First of all, I don't know how to open Open the DataFridViewPrinter.cs file, when I double clicked on it, I got a pop out window that said "Windows cannot open this file. To open this file, Windows need to know bla blah..."
And then in step2, you said add "Public modifier to the class", how do I do that? Type public class DataGridViewPrinter in a form?
Step3.
"csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs"
I am very sorry, I really don't know how to compile it. Is there a way to change DataGridViewPrinter.cs file into .dll and save it in the Framework folder so that I can do what you said in step4?
Step5
May I know why do we need to drag and drop DataGridView1(I presume it is from the tool box?) and also write the following code?
' Retrieve data from database and display on DataGridView. |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
Dim con As SqlConnection = New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=|DataDirectory|\SqlDatabase.mdf") |
Dim cmd As SqlCommand = New SqlCommand("SELECT ID, FirstName, LastName, Address FROM CustomerTable", con) |
con.Open() |
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd) |
Dim myDataSet As DataSet = New DataSet() |
myDA.Fill(myDataSet, "MyTable") |
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView |
con.Close() |
con = Nothing |
End Sub |
I am getting very confused because I just had to drop the DataGridView from the DataSources(on the solution explorer) and paste it in the form, then it would automatically create a datagridview table for me. Thanks again.
ps: Please don't get annoyed with me for asking so many questions.
Monday, February 23, 2009 3:03 AM
wnd4459 said:
First of all, I don't know how to open Open the DataFridViewPrinter.cs file, when I double clicked on it, I got a pop out window that said "Windows cannot open this file. To open this file, Windows need to know bla blah..."
If you installed Visual C# Express Edition or Visual Studio full Edition, then you can double-click the DataFridViewPrinter.cs file to open it and edit it.
wnd4459 said:
And then in step2, you said add "Public modifier to the class", how do I do that? Type public class DataGridViewPrinter in a form?
using System.Data;
using System.Windows.Forms;
public class DataGridViewPrinter
{
//......
}
wnd4459 said:
Step3.
"csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs"
I am very sorry, I really don't know how to compile it. Is there a way to change DataGridViewPrinter.cs file into .dll and save it in the Framework folder so that I can do what you said in step4?
Assume that you have installed Visual Studio full Edition or .NET Framework SDK, then Visual Studio Command Line tool is available.
Start Menu -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools - > Visual Studio 2008 Command Prompt -> Type the above command "csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs"
wnd4459 said:
Step5
May I know why do we need to drag and drop DataGridView1(I presume it is from the tool box?) and also write the following code?
I am getting very confused because I just had to drop the DataGridView from the DataSources(on the solution explorer) and paste it in the form, then it would automatically create a datagridview table for me. Thanks again.
Yes, I meant that drag&drop a DataGridView from Toolbox to Form1, in order to generate a DataGridView1 object, then binding database table to DataGridView1 programmatically.
Your way is feasible and more convenient, which automatically generates a DataGridView object and binding to database table via Data Source Wizard.
Glad to hear from you.
I have sent the DataGridViewPrinter.dll and my sample project to you. Please check them.
Regards,
Martin
Friday, May 1, 2009 1:14 AM
**gr8 Code from Martin Xie..
thx**
Thursday, August 13, 2009 8:07 AM
Hey guys, does this code work in VS2005?
I can't find the command prompt to convert the cs file to .dll =/
Any help would be awesome.
Thanks,
gnxc
Monday, August 17, 2009 4:59 AM
Hey guys, does this code work in VS2005?
I can't find the command prompt to convert the cs file to .dll =/
Any help would be awesome.
Thanks,
gnxc
Hi gnxc,
The DataGridViewPrinter.cs file still can work in VS2005/.NET2.0. Please follow the walkthrough mentioned in my post to use the DataGridViewPrinter component.
You can follow the steps to open Visual Studio Command Prompt, then compile the DataGridViewPrinter.cs file to a dll.
Start menu -> All Programs -> Microsoft Visual Studio 2005 -> Visual Studio Tools -> Visual Studio 2005 Command Prompt
Walkthrough:
- Download the class (DataGridViewPrinter.cs) file - 4.2 Kb
2. Open the DataGridViewPrinter.cs file, add "Public" modifier to the class like this:
public class DataGridViewPrinter
so that this library can be accessed in external assemblies.
3. Compile the DataGridViewPrinter.cs file into a dll in command line:
csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs - Add reference the dll into your project
Project menu -> Add Reference -> Browser -> Locate C:/DataGridViewPrinter.dll and add it - Code sample:
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback on our support, please contact msdnmg@microsoft.com
Wednesday, August 26, 2009 7:42 AM
It works, thanks so much!
Friday, August 28, 2009 6:28 AM
Martin
I downloaded the cs from CodeProject, opened it and added the Public modifier.
However, I'm using VS2008 Express Edition and cannot find the Visual Studio Tools folder, nor the VS2008 Command Prompt anywhere in the VS2008 folder.
What else can I do to use the DataGridViewPrinterClass?
Thanks for any help with this.
Friday, August 28, 2009 10:04 AM | 1 vote
Hi jwavila,
Glad to see you here.
Could you please leave your email here? I will send the DataGridViewPrinterClass.dll to you :)
Best regards and wishes to you.
v-maxie@microsoft.comPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback on our support, please contact msdnmg@microsoft.com
Saturday, August 29, 2009 9:41 AM
Hello Martin,
I tried the codes, imported dll file.
I passed sql connections i am using dataset wizard in vb .net so i want to add only print datagrid code to my program.
However code returns nothing when i pressed print or neither preview buttons.
I had a datagridview table i only integrated upper VB code with dll extension , not the check buttons.
Thx & Best Regards
Salih
Code is Below;
Imports System.Drawing.Printing
Public Class Form1
Dim MyDataGridViewPrinter As DataGridViewPrinter
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As PrintDialog = New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = False
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
If MyPrintDialog.ShowDialog() <> DialogResult.OK Then
Return False
End If
PrintDocument1.DocumentName = "saves"
PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
PrintDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
PrintDocument1.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)
If MessageBox.Show("Report ?", "Center", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
MyDataGridViewPrinter = New DataGridViewPrinter(SALIH1DataGridView, PrintDocument1, True, True, "saves", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Else
MyDataGridViewPrinter = New DataGridViewPrinter(SALIH1DataGridView, PrintDocument1, False, True, "saves", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
End If
Return True
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SALIH1TableAdapter.Fill(Me.SALIH1DataSet.SALIH1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SetupThePrinting() Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
If more = True Then
e.HasMorePages = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SetupThePrinting() Then
Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
MyPrintPreviewDialog.Document = PrintDocument1
MyPrintPreviewDialog.ShowDialog()
End If
End Sub
End Class
Friday, January 8, 2010 4:52 PM
Hello Martin, You seem like a guy who know's what you're doing.. i've got a slight problem... if you could help me that would be awesome thank you....
I'm using 3 tables, Primary, Secondary and Tertiary,
how do i get all three datagridview's to show for print preview and print on one form?? Any idea what the code may be as i have no idea because I have never done a print procedure for datagridview's on a form before.. but i want to show all three datagrids of the stock items that values have been changed from each datagrid on the one form...
Sorry if this doesn't make sense. if it doesn't, please get back to me and i will try and make it a bit more clearer..
Thanks in advanced
Harvey
Friday, February 12, 2010 2:46 AM
Hi wnd,
You're welcome.
I use the DataGridViewPrinter class to create a sample application successfully, which can print a DataGridView.
Martin Xie - MSFT said:
2. The DataGridViewPrinter Class
http://www.codeproject.com/KB/printing/datagridviewprinter.aspx
![]()
Walkthrough:
- Download the class (DataGridViewPrinter.cs) file - 4.2 Kb
2. Open the DataGridViewPrinter.cs file, add "Public" modifier to the class like this:
public class DataGridViewPrinter
so that this library can be accessed in external assemblies.
3. Compile the DataGridViewPrinter.cs file into a dll in command line:
csc /t:library /out:C:/DataGridViewPrinter.dll C:\DataGridViewPrinterClass\DataGridViewPrinter.cs- Add reference the dll into your project
Project menu -> Add Reference -> Browser -> Locate C:/DataGridViewPrinter.dll and add it- Code sample:
Prerequisites: Drag&drop DataGridView1, PrintDocument1, Button1 and Button2 onto Form1.Imports System.Data.SqlClient
Imports System.Drawing.Printing
Public Class Form1
'Create DataGridViewPrinter type variable
Dim MyDataGridViewPrinter As DataGridViewPrinter
' Retrieve data from database and display on DataGridView.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As SqlConnection = New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=|DataDirectory|\SqlDatabase.mdf")
Dim cmd As SqlCommand = New SqlCommand("SELECT ID, FirstName, LastName, Address FROM CustomerTable", con)
con.Open()
Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
'The Print button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SetupThePrinting() Then
PrintDocument1.Print()
End If
End Sub
'The PrintPage action for the PrintDocument control
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim more As Boolean = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
If more = True Then
e.HasMorePages = True
End If
End Sub
'The PrintView button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SetupThePrinting() Then
Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
MyPrintPreviewDialog.Document = PrintDocument1
MyPrintPreviewDialog.ShowDialog()
End If
End Sub
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As PrintDialog = New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = False
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
If MyPrintDialog.ShowDialog() <> DialogResult.OK Then
Return False
End If
PrintDocument1.DocumentName = "Customers Report"
PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
PrintDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
PrintDocument1.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)
If MessageBox.Show("Do you want the report to be centered on the page", "InvoiceManager - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, True, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Else
MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
End If
Return True
End Function
End Class
If you feel interested, please leave your email, I will send the DataGridViewPrinter.dll and my sample project to you for checking.
v-maxie@microsoft.com
Hi Maxie:
I have a question: this solution work to web pages? I have a website and I need to print my GridView that is inside a MasterPage. Sorry by my english. My email is tatinccr@hotmail.com. Thank you so much.
Monday, February 22, 2010 9:38 AM
hi.. this is a great help sir, but i can't follow step1 - step3. can you please send to me the datagridviewprint.dll so that we could proceed to step 4? thank you so much.. kindly send it to my email leih.espinoza@gmail.com..
Tuesday, May 18, 2010 6:26 AM
i cant do it code
Dim MyDataGridViewPrinter As DataGridViewPrinter
says datagridviewprinter not accessible because its friend what that even mean
Friday, August 13, 2010 11:54 PM
Hi,
This is the topic i've been looking for ^_^
Can I still have the .dll for this project for VB (Visual Studio 2008 Pro)
efren.vergara@live.com
I try to email Martin but it does not allow me to send email to him. Im looking forward to hear from them.
Thank you! Xie Xie
Monday, November 8, 2010 4:04 PM
Hi, i am trying to print from a grid view as well but its not printing anything but a blank page here is my code.
Public Class frmViewDebitOrderPolicies
' The DataGridView Control which will be printed.
'Dim vwDebitOrderClientsDataGridView As New DataGridView
' The PrintDocument to be used for printing.
Dim MyPrintDocument As New PrintDocument
' The class that will do the printing process.
Dim DataGridViewPrinter As DataGridViewPrinter
Private Sub frmViewDebitOrderPolicies_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Try 'TODO: This line of code loads data into the 'ClientsDs.vwDebitOrderClients' table. You can move, or remove it, as needed.
Me.VwDebitOrderClientsTableAdapter.Fill(Me.ClientsDs.vwDebitOrderClients)
End Sub
Private Sub VwDebitOrderClientsDataGridView_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles VwDebitOrderClientsDataGridView.CellContentClick
If e.ColumnIndex = 11 Then
Dim clientID As Integer = Integer.Parse(CType(sender, DataGridView).Rows(e.RowIndex).Cells(0).Value)
Dim frmApps As New frmApplication()
frmApps.MdiParent = Me.MdiParent
frmApps.clientID = clientID
frmApps.OperationActionMode = frmApplication.OperationMode.EditDetail
frmApps.Show()
Me.Close()
Else
If e.ColumnIndex = 12 Then
Dim clientID As Integer = Integer.Parse(CType(sender, DataGridView).Rows(e.RowIndex).Cells(0).Value)
ClientDetail.DeleteClientDetail(clientID)
VwDebitOrderClientsDataGridView.Invalidate()
VwDebitOrderClientsDataGridView.Refresh()
End If
End If
End Sub
' The print Button
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click
If SetupThePrinting() Then
PrintDocument1.Print()
End If
End Sub
' The PrintPage action for the PrintDocument control
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim more As Boolean = DataGridViewPrinter.DrawDataGridView(e.Graphics)
If more = True Then
e.HasMorePages = True
End If
End Sub
' The Print Preview Button
Private Sub btnPrintPreview_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrintPreview.Click
If SetupThePrinting() Then
Dim MyPrintPreviewDialog As New PrintPreviewDialog()
MyPrintPreviewDialog.Document = PrintDocument1
MyPrintPreviewDialog.ShowDialog()
End If
End Sub
' The printing setup function
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = False
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
If MyPrintDialog.ShowDialog() <> DialogResult.OK Then
Return False
End If
PrintDocument1.DocumentName = "Clients Report"
PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
PrintDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
PrintDocument1.DefaultPageSettings.Margins = New Margins(40, 40, 40, 40)
If MessageBox.Show("Do you want the report to be centered on the page", "InvoiceManager - Center on Page", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
DataGridViewPrinter = New DataGridViewPrinter(VwDebitOrderClientsDataGridView, PrintDocument1, True, True, "Clients", New Font("Tahoma", 16, FontStyle.Bold), Color.Black, True)
Else
DataGridViewPrinter = New DataGridViewPrinter(VwDebitOrderClientsDataGridView, PrintDocument1, False, True, "Clients", New Font("Tahoma", 16, FontStyle.Bold), Color.Black, True)
End If
Return True
End Function
End Class
Friday, March 4, 2011 4:22 AM
how about use access database???
Thursday, March 20, 2014 7:21 AM
Hi Martin, I'm working on a project right now and Im also trying to print my datagridview and tried to follow your steps but im using windows 8 and dont know how to convert the datagridviewprinter.cs to a .dll file.. would you be kind enough to send it to me? thanks
Wednesday, October 22, 2014 8:53 AM
Hi,
Can you send me the dll file in my email johnallandevilla@gmail.com/
Thank you!
Thursday, February 25, 2016 7:07 AM
hi can you email me the dll file. whenever im compiling it there's an error CS2012. please. yukino_048@yahoo.com is my email. please
Thursday, February 25, 2016 7:09 AM
hi can you please email me the dll file? Whenever im compiling it theres an error.
my email is yukino_048@yahoo.com or derikrim048@gmail.com any email will do pls.
thanks in advance
Thursday, February 25, 2016 7:12 AM
Hi can you please email the dll file to me. There's an error wherever Im compiling it.
here's my email: yukino_048@yahoo.com or derikrim048@gmail.com.
Thank you in advance I needed it so badly.
Sunday, March 27, 2016 5:00 PM
japhetbatucan@gmail.com your project will be very helpful to me. :D
Wednesday, August 21, 2019 8:39 AM
THIS CODE FOR c# ,how can i using this class "DataGridViewPrinter" with VB.net
Wednesday, August 21, 2019 11:48 AM
Hi
If you start a new thread, I can post VB.NET code from an MS site to print a DataTable (via a DataGridView).
Regards Les, Livingston, Scotland