|
|
Comments and Discussions
|
|
 |

|
Can anyone see my entry dated the 27/12/2012. I have not seen any response to that entry.
regards Ron.
|
|
|
|

|
I had, and had thought I'd emailed you about it - however, I can't find the outgoing email. Perhaps my memory isn't what it used to be? Anyway, I'm on vacation at the moment. I'll look into this when I return. Thanks for reminding me about this one!
Steve G.
|
|
|
|

|
Hi Steve,
I have a DGV that for some columns containing numeric data I have the defaultcellstyle.alignment set to MiddleRight and the defaultcellstyle.padding set to e.g. 0,0,10,0 (padding to right of value only). This appears to cause DGVPrinter to draw the right border for that column nearer the cell value rather than drawing the right border in its correct place taking into account the padding applied to the cell value.
e.g. given three cells of which the middle one is the right-justified padded one:
Should be drawn as: | xxxx| 1234 |xxxx
is in fact drawn as: | xxxx| 1234| |xxxx
Any ideas?
Cheers
Derek
modified 10 Apr '13 - 13:56.
|
|
|
|

|
That sounds very much like a bug. I'm on vacation and will take a look at things when I return. Thanks for reporting this!
Steve G.
|
|
|
|

|
Thank you very much for such a SIMPLE and EASY article for printing GRIDVIEW....
I was searching for this very desperately, but all I found very complex codes, I used them, but there were some part of code snippet unexplained, making it hard to use.
But your code was very simple to implement...THANKS A LOT!
|
|
|
|

|
Just wanted to drop a few lines to thank the author for this excellent piece of code! It is well thought out and easy to implement. Again, many many thanks!
|
|
|
|

|
It's great works excellent and too easy to use
|
|
|
|

|
First, Let me say that I'm sure that this is the best DataGridView Printer ever made.
I just need some help concerning drawing text inside headers with a 270 angle. Apparently I'm not able to subscribe to the OwnerDraw event. What does this mean? print.OwnerDraw += new CellOwnerDrawEventHandler(print_OwnerDraw). Is there anything missing?
|
|
|
|

|
First, my apologies for not responding sooner - I've just discovered that some of the post emails from CodeProject are going into my junk folder!
To answer your question, the statement you list there registers a handler for the OwnerDraw event. The argument "print_OwnerDraw" is actually the name of a procedure that will get called when the OwnerDraw event is triggered. You write this routine. The code in this routine is your opportunity to do what you need to to handle the OwnerDraw event. I'd strongly suggest you search the internet for help on handling events - there's lots of examples out there to help you wire up the event handler properly.
Good Luck,
Steve G.
|
|
|
|

|
Currently when the row header is printed the Row Style is used as in
//-----------------------------------------------------------------
// Print Row background
//-----------------------------------------------------------------
// get current row style, start with header style
DataGridViewCellStyle rowstyle = row.InheritedStyle.Clone();
Should rowstyle not be defined as
DataGridViewCellStyle rowstyle = row.HeaderCell.InheritedStyle.Clone();
The row headers will then have the same look as the datagridcview.
regards ron
|
|
|
|

|
Good catch, but actually, the change needs to be made in the code just below there - where the header cell is printed.
Thanks, I've added this to the list of fixes for the next release.
Steve G.
|
|
|
|

|
I have 3 suggestion.
1. can u add some custom row in a doc end? for display the total price, percentage as well as some tips. before i think add this to DataGridView and print, but when print is finish , i should clear that row. if i can add multiple DataGridView, and you combine this ? , it's so easy for me.
2.is there any way can let the column width automatically populated? if i'm use DGVPrinter.ColumnWidthSetting.CellWidth or DGVPrinter.ColumnWidthSetting.Porportional Its width may exceed the page, if i'm use ColumnWidthSetting.DataWidth It will be remaining lot of space. can u just let It automatically calculates the width to fit the page? (Beyond the automatic line feed)?
3.if i'm print , i only can print vertical mode on A4. can i choise the print mode like horizontal?
very thanks.
|
|
|
|

|
Excellent piece of work. It only takes a small change to make it compatible back to Dot Net 2.0. Online conversion from C# to VB.net also worked cleanly.
|
|
|
|

|
Does anyone have a VB.NET conversion?
|
|
|
|

|
Hi,
I need to make my own print preview page to use a Ribbon Control like in Paint and show the print preview not in a new dialog form.
Here my code:
Private Sub PT_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PT.PrintPage
Dim printer = New DGVPrinter()
Dim prflex As DataGridView
prflex = XpTabControl1.SelectedTab.Controls(0)
printer.RowHeight = DGVPrinter.RowHeightSetting.CellHeight
printer.EmbeddedPrint(prflex, e.Graphics, New Rectangle(10, 10, prflex.Width, prflex.Height))
End Sub
After this I set
PrintPreviewControl1.Document = PT
I have problems to translate the Embedded Printing Process for Multipage Printing C# code to VB.net, anybody just used this feature in VB?
Here a link to my print preview
https://dl.dropbox.com/u/39631349/preview_Another%20DataGridView%20Printer.png[^]
Thanks, Thomas
|
|
|
|

|
When the datagridview is wider than a printed page and the RowHeight setting is CellHeight then printing is fine irrespective of 'porportional' setting.
But when RowHeight is set to DataHeight then the height of the rows seems to depend upon the largest text string within that row even though the text is not wrapped.
regards ron.
further to the problem above
the problem seems to be the connection between AdjustPageSets and RecalcRowHeights. the routine AdjustPagesets is entered with a pageset which calls RecalcRowHeights with the number of columns within that pageset. On further pagesets RecalcRowHeights starts off looking at columns within the first pageset and because the column widths are different the heights of the rows are adjusted even though data in cells is not wrapped.
In the routine calccellsize after the else statement I put in a mod because a checkboxcell returns an invalid height when g.MeasureString(String,Font,Size,format) is used, at least it does on my system.
else
{
if (cell is DataGridViewColumnHeaderCell)
datasize = g.MeasureString(cell.OwningColumn.HeaderText.ToString(), cellstyle.Font,
new SizeF(basewidth, maxPages), format);
else
{
if ("DataGridViewCheckBoxCell" == dgv.Columns[cell.ColumnIndex].CellType.Name)
datasize = g.MeasureString(cell.EditedFormattedValue.ToString(), cellstyle.Font);
else
datasize = g.MeasureString(cell.EditedFormattedValue.ToString(), cellstyle.Font,
new SizeF(basewidth, maxPages), format);
}
// if we have excessively large cell, limit it to one page width
if (printWidth < datasize.Width)
datasize = g.MeasureString(cell.FormattedValue.ToString(), cellstyle.Font,
new SizeF(pageWidth - cellstyle.Padding.Left - cellstyle.Padding.Right, maxPages),
format);
}
happy new year readers
further to the problem between AdgustPageSets and RecalcRowHeights i put in a mod
int prevColCount = 0;
for (i = 0; i < pagesets.Count; i++)
{
AdjustPageSets(g, pagesets[i], prevColCount);
prevColCount += pagesets[i].colwidths.Count;
}
and within AdjustPageSets
private void AdjustPageSets(Graphics g, PageDef pageset, int prevcolCount)
{
.
.
RecalcRowHeights(g, i + prevcolCount, pageset.colwidths[i]);
.
}
this rough and ready mod fixed the problem of rowheights along with the calccellsize.
regards Ron
modified 2 Jan '13 - 9:40.
|
|
|
|
|

|
I'm trying to recreate the issue you found I'm not sure I'm following you. Are you saying that when you hid the first column, that all the row headers were not displayed?
-- Edit --
After a bit of poking about, I've found the issue. It will be fixed in the next update. Thanks for reporting this!
-- Edit --
Thanks,
Steve G.
modified 24 Dec '12 - 13:43.
|
|
|
|
|

|
Good work! But code formating...
|
|
|
|

|
My code is perfectly formatted ... for me.
Seriously, thanks for the good vote.
|
|
|
|

|
Throws exceprion "Argument out of range..." in:
calccellsize(g, col.HeaderCell, i, headercolstyle, usewidth, columnheadercellformat);
call, when trying get property cell.EditedFormattedValue.
MSDN, says:
Exception Condition
ArgumentOutOfRangeException The row containing the cell is a shared row.
-or-
The cell is a column header cell.
Why, then, this is not happening all the time, but only from a specific table?
|
|
|
|

|
The problem was solved in this way:
if (cell is DataGridViewColumnHeaderCell)
datasize = g.MeasureString(cell.OwningColumn.HeaderText.ToString(),cellstyle.Font,
new SizeF(basewidth, maxPages), format);
else
datasize = g.MeasureString(cell.EditedFormattedValue.ToString(), cellstyle.Font,
new SizeF(basewidth, maxPages), format);
And so in all the places where you are trying to get EditedFormattedValue.
Also further found another bug:
CheckBoxState state = (checkboxcell.Value == null || (Boolean)checkboxcell.Value == false) ?
CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
Skipped check for DBNull. After change:
CheckBoxState state = (checkboxcell.Value == null || checkboxcell.Value == DBNull.Value || (Boolean)checkboxcell.Value == false) ?
CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
|
|
|
|

|
... Oh, and good catch on the DBNull for checkboxes!
Steve G.
|
|
|
|

|
"Why, then, this is not happening all the time," is an excellent question. I'm wondering how your table was set up that triggered this error. Personally, I've not seen this so any info you can provide would be useful.
Steve G.
|
|
|
|

|
Nice program but have encounted two problems.
1. CheckBoxState state = (checkboxcell.Value == null || (Boolean)checkboxcell.Value == false) ? CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
gives exception below
System.InvalidCastException was unhandled by user code
Message=Specified cast is not valid.
replaced statement with
CheckBoxState state;
if (Convert.ToBoolean(checkboxcell.Value)) state = CheckBoxState.CheckedNormal;
else state = CheckBoxState.UncheckedNormal;
problem went away.
2. when checkbox checked, height returned from calccellsize datasize.height = 18.++ for checkbox.
datasize = g.MeasureString(cell.EditedFormattedValue.ToString(), cellstyle.Font,
new SizeF(basewidth, maxPages), format);
when checkbox unchecked datasize.height returns 30.++
which results in different row heights when grid previewed or printed.
Font = tahoma 9.75. differents. font size smaller seem ok.
Cant help feeling something not setup correctly in my checkbox setup.
regards Ron.
|
|
|
|

|
Interesting issue on the checkbox "invalid cast" error, and the checkbox sizing - can you give me more information on the error (i.e. what value cased the problem) and how your checkboxes were set up?
Thanks,
Steve G.
|
|
|
|

|
Invalid cast accurs when checkbox.value returns "true" or "false".
datagridview unbound with Rows initialised from a csv file with the string values "true" or "false" entered for the checkbox column.
Using Microsoft Visual C# 2010 Express. Datagridview and columns setup via designer. Checkbox column setup by TrueValue = "true",
FalseValue = "false". Flatstyle=standard, ThreeState=false, no other settings amended.
There are three datagridviews but only one that is initialisd from a file, the other two depend one the data from the first. these two grids always appear to have a row hieght approx twice what they need to have whist the first has different row hieghts depending on wether "true" or "false".
hope this enables you to progress
regards Ron
|
|
|
|

|
Found it! The issue is setting "TrueValue" and "FalseValue" to something other than true or false. I've fixed your issue and expanded the code to handle Tristate checkboxes as well. I'll be posting a fix soon, but if you can't wait you can change the code that gets the checkbox state in DrawCheckBoxCell() from
CheckBoxState state = (checkboxcell.Value == null || (Boolean)checkboxcell.Value == false) ?
CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
to
CheckBoxState state = CheckBoxState.UncheckedNormal;
if (checkboxcell.ThreeState)
{
if (((CheckState)checkboxcell.EditedFormattedValue) == CheckState.Checked)
state = CheckBoxState.CheckedNormal;
else if (((CheckState)checkboxcell.EditedFormattedValue) == CheckState.Indeterminate)
state = CheckBoxState.MixedNormal;
}
else
{
if ((Boolean)checkboxcell.EditedFormattedValue)
state = CheckBoxState.CheckedNormal;
}
|
|
|
|

|
Many thanks for the fix which will take care of three state checkboxes, althought I had switched off 3 state with threestate = false. The routine within the
if (checkboxcell.ThreeState) is never entered when I debug the program.
it appears that changing the original
// determine checked or notchecked
CheckBoxState state = (checkboxcell.Value == null || (Boolean)checkboxcell.Value == false) ?
CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
to
// determine checked or notchecked
CheckBoxState state = (checkboxcell.Value == null || (Boolean)checkboxcell.EditedFormattedValue == false) ?
CheckBoxState.UncheckedNormal : CheckBoxState.CheckedNormal;
does not give a problem.
regards Ron.
|
|
|
|

|
Great Work - Thank you for sharing
|
|
|
|

|
I see you've started versioning your tool. That's a good idea, but I don't know how to tell what version I have now.
The subfolders I have in my "DGVPrinter" folder are:
* 2008-09,
* 2009-09,
* 2010-01,
* 2010-08,
* 2011-04-25,
* 2011-05-17 beta,
* 2011-06-02 beta, and now
* 2012-03-26
FYI: If you need any of the old code from way back, I guess I've got copies!
|
|
|
|

|
If you look up near the top of this page, you'll see a link for revisions. If you click on this you can see that the most current version was published in March of this year, so your 2012-03-26 version is the latest and greatest.
Steve G.
P.S. Have there really been 34 versions??!!!?
|
|
|
|

|
Wow. 34.
Actually, I just downloaded Version [2012-03-26], and I was looking to see what all had happened since Version [way, way back].
I spent some time working with the new version and reading the PDF. The KeepRowsTogether property is new. Can this (or some other technique) be used to "shrink" my grid to fit 1 page? Visual Studio has a Control.Scale method that takes a SizeF to resize all of the controls by that amount. I can add a public void Scale(SizeF value); method to your code, but where would be a good spot to calculate how wide a DataGridView sheet is in relation to the Selected Printer's PageSettings.PaperSize.Width?
I constantly get complaints from our employees (who don't know how to do anything besides pushing the print button) when an empty column or two gets sent to the printer on a separate page.
|
|
|
|

|
Any example for how to place a picture in the printing document?
Thanks Very Much
|
|
|
|

|
In the documentation, the section "DGVPrinter Image Properties" has the Image API and an example of placing an image in the printed area.
Steve G.
|
|
|
|

|
Thank U, I solved my issue!
|
|
|
|

|
aureolin, thank you for the excellent code. is a question which parameter is responsible for all the columns fit the width of the sheet in one?
picture print by default
http://img254.imageshack.us/img254/1185/dgvprint.png[^]
that is, if I stretch window to full screen, and since DataGridView align it to the window, and print what is given in the picture
http://img850.imageshack.us/img850/8571/dgvprinttest2.png[^]
How fixed?
settings:
Dim Printer = New DGVPrinter
Printer.Title = "TEST"
Printer.SubTitle = FormatDateTime(Date.Today, DateFormat.LongDate)
Printer.SubTitleFormatFlags = StringFormatFlags.LineLimit Or StringFormatFlags.NoClip
Printer.PageNumbers = True
Printer.PageNumberInHeader = False
Printer.ColumnWidth = DGVPrinter.ColumnWidthSetting.Porportional
Printer.HeaderCellAlignment = StringAlignment.Near
Printer.Footer = "TESTTESTEST"
Printer.FooterSpacing = 15
Printer.PrintDataGridView(Me.dgv_prih)
modified 12 Nov '12 - 1:06.
|
|
|
|

|
The setting that controls this is the "ColumnWidth" setting. You've set it to "Porportional", which will stretch the columns to fill the page. The other settings are "DataWidth" which will scale the column widths to fit the data, and "CellWidth" which will reflect the current display column widths to the printout. You can also set individual column widths to fixed values to help 'adjust' the printout to look the way you want it to.
Steve G.
|
|
|
|

|
Old ver.
gives me :A generic error occurred in GDI+.
Ver 4.2
gives me :Fixed row exceeds width error or something like this.
Help Please
|
|
|
|

|
???
I think I'm going to need more information. What was the specific error and where did it occur? Which of the available print methods were you using (assuming this error occurred during printing)?
Steve G.
|
|
|
|

|
I am sorry I thought I was clear ,
The error occurs when I tried to Print and preview DGV ofcourse
Printer.PrintPreviewDataGridView(DataGridView1)
The weird thing is the dgv was in separate form it was printing normally
when I put it inside tabcontrol tabpage the error appears just before the preview window shows up
A generic error occurred in GDI+.
I downloaded last ver 4.2 and I got now new error also on printing the dgv which I told you earlier.
modified 8 Nov '12 - 17:49.
|
|
|
|

|
I found the cause of this error.
It is not related to tab control or tab page.
I found that the selected printer is set to Microsoft OneNote ,once I changed it to normal printer the error is gone.
Sorry If I bothered you .
|
|
|
|

|
Not a problem.
Steve G.
|
|
|
|

|
Excellent piece of work. But how do you change the title, subtitle, & footer properties (e.g., bold, size, etc.)? Tks, OH
|
|
|
|

|
Methods to do this start on pg. 10 of the documentation. And, you just made me realize that I neglected to number the pages in the documentation. Heh - one more item for the next revision.
Steve G.
|
|
|
|

|
Welll, it wasn't exactly obvious that that was documentation. I was expecting something different and didn't exactly know that I had to do it as follows:
Printer.TitleFont = New Font("Tacoma", 14, FontStyle.Regular)
Maybe a snippet of code for each property would be illustrative (for us folks that are still trying to get our arms around this beast (VB.Net)). However, I am impressed that there is even 'documentation'.
But indeed it does work. Tks, OH
|
|
|
|

|
Dear author, is it possible somehow to add a row number for each row to the printed page? I know, that i can add a column with this information, but i think, that more people would appretiate, if this functionallity would be built-in. Example I have grid with this data Jane Doe Joe Doe I would like to have on the printed page 1 Jane Doe 2 Joe Doe If it is possible to do it know somehow, please let me know. Thank you and great job!
|
|
|
|

|
Had to think about this one for a while. At this point, I don't see this as a feature that fits within what DGVPrinter is trying to do. That said, this is a very creative and interesting suggestion - it really made me think!
Thanks for the suggestion -
Steve G.
|
|
|
|

|
I believe it's "Proportional" not "Porportional". Otherwise, your code was really easy to just plug-in and go. Ryan C. Price
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
DataGridView printing encapsulated in a stand-alone object. Very easy to use! Updated to allow printing columns wider than one page.
| Type | Article |
| Licence | CPOL |
| First Posted | 16 Mar 2007 |
| Views | 810,372 |
| Downloads | 23,392 |
| Bookmarked | 361 times |
|
|