-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Well,
The solution was easy enough. Figured I would post for others like me. Notice the margin assignment below.
Code:
With Me.PrintPreviewDialog1
.Document = GridPrinter.PrintDocument
.Document.DefaultPageSettings.Margins.Left = 50
.Document.DefaultPageSettings.Margins.Right = 50
If .ShowDialog = DialogResult.OK Then
GridPrinter.Print()
End If
End With
Dave
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Hey,
Why is it I get these errors. Im using visual studio 2005. Is there a way I can solve this printing problem please
Code:
Warning 2 Function without an 'As' clause; return type of Object assumed. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 410 21 DataGridPrinter
Warning 3 Unused local variable: 'x'. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 419 13 DataGridPrinter
Warning 4 Unused local variable: 'y'. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 419 26 DataGridPrinter
Warning 5
Function 'DrawCellString' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 441 5 DataGridPrinter
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Warning 2 Function without an 'As' clause; return type of Object assumed. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 410 21 DataGridPrinter
Because I am human and fallible I didn't put any return type on the function declared on line 410. In fact it should probably be changed to be a sub not a function?
Quote:
Warning 3 Unused local variable: 'x'. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 419 13 DataGridPrinter
Because I am human and fallible I didn't remove the variable declaration 'x' on line 419 when I removed the code that used it. It can be removed.
Quote:
Warning 4 Unused local variable: 'y'. C:\DataGridPrinter_src\DataGridPrinter\DataGridPrinter.vb 419 26 DataGridPrinter
Because I am human and fallible I didn't remove the variable declaration 'y' on line 419 when I removed the code that used it. It can be removed.
Quote:
Warning 5
Function 'DrawCellString' doesn't return a value on all code paths. A null reference exception could
Because I am human and fallible etc.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by GSIV
Thanks a lot for sharing this great work, Merrion! :)
I d/l'd your latest version and got it to work right off!
Hi,
Can I use it with VB6 application so that I can use it in my vb6 project to print the grids?
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
HI Merrion,
Can you please update the whole thing and is it possible for me (not being so much of an expert in vb.net) to print from a dataset and dataview?
Thanks and keep up the good work on this Forum.
cheers
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I am having a bit of a strange problem. I am trying to Print out a Datagridview using a PrintPreviewDialog. The Datagridview contains the contents of a table.
If I populate the table manually (row by row) everything is fine. If I populate the table using an access database and an OleDbDataReader then I can only see (and subsequently print out) the first page.
Someone mentioned in an earlier thread that the problem might be resolved by setting the e.HasMorePages property to true. Could someone tell me how to do this
thanks
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
This is the code that works
Code:
Dim MyTable As New DataTable
MyTable.Columns.Add(New DataColumn("Team", GetType(String)))
MyTable.Columns.Add(New DataColumn("Played", GetType(Integer)))
MyTable.Columns.Add(New DataColumn("Goal Difference", GetType(Integer)))
MyTable.Columns.Add(New DataColumn("Points", GetType(Integer)))
MyTable.Rows.Add(New Object() {"Shrewsbury", 4, 3, 9})
MyTable.Rows.Add(New Object() {"Chester", 4, 5, 8})
DataGridView1.DataSource = MyTable
If GridPrinter Is Nothing Then
GridPrinter = New DataGridPrinter(Me.DataGridView1)
End If
'setting properties of dgview (after adding gridprinter )
With GridPrinter
.HeaderText = CStr("Report Dated - " & Today)
.HeaderHeightPercent = CInt(5)
.FooterHeightPercent = CInt(0)
.InterSectionSpacingPercent = CInt(3)
.PagesAcross = CInt(2)
End With
'printpreview prinout
With Me.PrintPreviewDialog1
.Document = GridPrinter.PrintDocument
If .ShowDialog = DialogResult.OK Then
GridPrinter.Print()
End If
End With
This is the code that doesn't work
Code:
Dim MyTable As New DataTable
OleDbCommand1.CommandText = "SELECT Incident_ID, Incident_Date, Incident_Time, Location, Incident_Type, What_Happened, Affected_Individual, Police_Involvement, Crime_Number, Remedial_Action, Assailant FROM AccidentIncident"
OleDbCommand1.Parameters.AddWithValue("@Incident_ID", "1")
Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader()
MyTable.Load(reader)
reader.Close()
DataGridView1.DataSource = MyTable
If GridPrinter Is Nothing Then
GridPrinter = New DataGridPrinter(Me.DataGridView1)
End If
'setting properties of dgview (after adding gridprinter )
With GridPrinter
.HeaderText = CStr("Report Dated - " & Today)
.HeaderHeightPercent = CInt(5)
.FooterHeightPercent = CInt(0)
.InterSectionSpacingPercent = CInt(3)
.PagesAcross = CInt(2)
End With
'printpreview prinout
With Me.PrintPreviewDialog1
.Document = GridPrinter.PrintDocument
If .ShowDialog = DialogResult.OK Then
GridPrinter.Print()
End If
End With
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I am using Merrion's method for printing the contents of my datagridview. It works great. I do have one question though. With my data grid view, I have set the text that shows up for each row using DataGridView1.Rows(x).HeaderCell.Value. Is there a way to have these text items also show up in the printing of the datagridview contents?
Thanks!
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by
micki_free
This is the code that works
Code:
If GridPrinter Is Nothing Then
GridPrinter = New DataGridPrinter(Me.DataGridView1)
End If
'setting properties of dgview (after adding gridprinter )
With GridPrinter
.HeaderText = CStr("Report Dated - " & Today)
.HeaderHeightPercent = CInt(5)
.FooterHeightPercent = CInt(0)
.InterSectionSpacingPercent = CInt(3)
.PagesAcross = CInt(2)
End With
'printpreview prinout
With Me.PrintPreviewDialog1
.Document = GridPrinter.PrintDocument
If .ShowDialog = DialogResult.OK Then
GridPrinter.Print()
End If
End With
I'm trying to use this code in VB.NET 2008 and it has no clue what 'GridPrinter' is. Am I missing something? was I supposed to import a class or something? Thanks for any help.
Name 'GridPrinter' is not declared.
Type 'DataGridPrinter' is not defined.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Look at post #50 in this thread as it defines GridPrinter.
But, if you have DataGridPrinter undefined, then you have not included the reference to your solution.
I would suggest go back and read Merrions post. There is also more info at the CodeProject on his package.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Alright, I took care of those errors but I have a slight issue
The class says it can't convert my DataGridViews to DataGrids...so this class was made for vb 2005. I tried setting it to DataGridView instead of DataGrid but TableStyles no longer exists in the DataGridView
Value of type 'System.Windows.Forms.DataGridView' cannot be converted to 'System.Windows.Forms.DataGrid'.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Come on now....
A line like this does define a variable.
Private WithEvents GridPrinter As DataGridPrinter
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I have a temporary work around... I copied the DataGrid from his VB 2003 document and pasted it into my VB 2008 document. I've hidden it outside of my form and the printer class is feeding from that DataGrid. If anyone knows how to fix it for a DataGridView, that would be great.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I added this lines in the form load of DataGridPrinter by Merrion
added code Code:
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Network_Info.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select DateRecorded As [Date Recorded], Format([TimeRecorded],'HH:MM:SS AM/PM') As [Time Recorded], Computer_Name As [Computer Name], Fastest_Download_Speed & ' Kb/s' As [Fastest Download Speed] , Fastest_Upload_Speed & ' Kb/s' As [Fastest Upload Speed], Average_Download_Speed & ' Kb/s' As [Average Download Speed], Average_Upload_Speed & ' Kb/s' As [Average Upload Speed], Total_Downloads & ' KB' As [Total Downloads], Total_Uploads & ' KB' As [Total Uploads], IP_Address, ElapsedTime As [Elapsed Time] from Computer_Connection_Info Order by DateRecorded, TimeRecorded", myConnection)
' create a new dataset
Dim ds As DataSet = New DataSet
' fill dataset
da.Fill(ds, "Computer_Connection_Info")
' write dataset contents to an xml file by calling WriteXml method
' Attach DataSet to DataGrid
DataGrid1.DataSource = ds.DefaultViewManager
But I always get an error in these lines:
code Code:
For nextLine = _CurrentPrintGridLine To Min((_CurrentPrintGridLine + RowsPerPage(_PrintFont, e.Graphics)), CType(_DataGrid.DataSource, System.Data.DataTable).DefaultView.Count)
Call PrintGridLine(e, nextLine)
Next
It says:
Unable to cast object of type 'System.Data.DataViewManager' to type 'System.Data.DataTable'.
I really don't understand what's going wrong.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
The DataGridPrinter only prints dataGrids whos data source is a DataTable.
Code:
DataGrid1.DataSource = ds.Tables(0)
See post 31.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by
Merrion
Yes - if you change the page layout (in print preview) then the columns and rows get recalculated to fit the page 'automatically'...
New version (updated with the article) supports background colours for the grid elements.
Am working on some more defect fixes and enhancements and will post the next release on this thread.
where exactly are you gonna do this? change the page layout to landscape.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by
korae
where exactly are you gonna do this? change the page layout to landscape.
I already figured it out, This is what I did:
With Me.PrintPreviewDialog1
.Document = GridPrinter.PrintDocument
.Document.DefaultPageSettings.Landscape = True
If .ShowDialog = DialogResult.OK Then
GridPrinter.Print()
End If
End With
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I wonder if someone could possibly assist, I am very new to VB (2008 Express) and am having some difficulty I am trying to print the contents of a DataGridView1 which has its source defined as Me.DataGridView1.DataSource = config.Tables(0). With config being a dataset that has been previously filled.
I have imported the DataGridViewPrinter VB code into the project.
With the actual form I have added Imports appname.DataGridViewPrinter to the top of the form that displays the datagrid.
I have also added the code described in post #20 and have the same errors. I have corrected by adding the following to the form that display & Prints the dataview
Code:
Dim MyPrintDocument As PrintDocument
Dim MyDataGridViewPrinter As DataGridViewPrinter
However, I am still getting 'MyPrintDocument.' , 'MyDataGridViewPrinter.', MyDataGridView.' & 'MyConstants.' as Name is not declared
Quote:
MyPrintDocument.DocumentName = "Customers Report"
MyPrintDocument.PrinterSettings = MyPrintDialog.PrinterSettings
MyPrintDocument.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings
MyPrintDocument.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(MyDataGridView, MyPrintDocument, True, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Else
MyDataGridViewPrinter = New DataGridViewPrinter(MyDataGridView, MyPrintDocument, False, True, "Customers", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
End If
Can someone please assist and explain what I am doing wrong.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I have managed to resolve the issue with MyprintDocument & MyDataGridViewPrinter not being declared, but within the DIM statement
Code:
Dim MyPrintDocument As PrintDocument
Printdocument is Now not defined.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
You need to tell the .NET framework where to fiund the PrintDocument class.
Try:-
Code:
Dim MyPrintDocument As System.Drawing.Printing.PrintDocument
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Is the source code on The Code Project page really the most up to date version? That was 4 years ago, and i see a lot of revisions in this thread.
.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Possibly not - although it has not changed at all in the last 3 years or so...06-04-2006 is the latest source on my mchine.
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Where do I find the latest version of this ???
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
I have got the DataGridViewPrinter.zip but am having trouble implementing it. Is there a sample project that uses this class for me too get some ideas off?
thanks
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
OK, I am playing around with the code. I have a couple of errors:
MyDataGridViewPrinter' is not declared and 'MyPrintDocument' is not declared.
Where do I declare these variables, and what is the syntax?
thanks
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Make sure you are opening the solution file (*.sln) not one of the projects (*.vbp) as in the latter case the references might not be loaded...
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by
Simon Canning
OK, I am playing around with the code. I have a couple of errors:
MyDataGridViewPrinter' is not declared and 'MyPrintDocument' is not declared.
Where do I declare these variables, and what is the syntax?
thanks
Did you figure this out? I have exactly the same problem, I added the .vb file to my project and added the code in the .txt file as well but I got the same errors as you...
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Hello Genius "Merrion" where can i get the latest version ?
Thanks
-
Re: DataGridPrinter - A class to print data grid in a nicely formatted way
Quote:
Originally Posted by
skea
Thanks to Berthold Simon, who converted the C# code.
Thank you for posting this code. when I print the grid, the rows are printing with black shaded text and not able to read the data. can you help me?