Problem printing DataGridView

J

Jeff

..net 2.0

I'm having trouble implementing printing of a DataGridView. Below is the
PrintPage event. The problem is that it doesn't matter how many pages the
DataGridView content spans, the code below will print the first page only. I
want all pages to be printed.

What am I missing here?

private void print_PrintPage(object sender, PrintPageEventArgs ev)
{
Font font = new Font("Arial", 10);
Font title = new Font("Arial", 20);
float leftMargin = ev.MarginBounds.Left;
float linesPerPage = ev.MarginBounds.Height /
font.GetHeight(ev.Graphics);
int count = 0;
float yPos = ev.MarginBounds.Top + title.GetHeight(ev.Graphics) + 750;

while (count < linesPerPage && count < grid1.Rows.Count)
{
DataGridViewRow row = grid1.Rows[count];
String contents;
if (row.Cells[0].Value == null)
{
contents = "";
}
else
{
contents = row.Cells[0].Value.ToString();
}
ev.Graphics.DrawString(contents, font, Brushes.Black, leftMargin,
yPos);
contents = null;

if (row.Cells[1].Value == null)
{
contents = "";
}
else
{
contents = row.Cells[1].Value.ToString();
}
ev.Graphics.DrawString(contents, font, Brushes.Black, leftMargin +
110, yPos);
yPos += font.GetHeight(ev.Graphics);
count++;
}
if (count >= (grid1.RowCount))
{
ev.HasMorePages = false;
}
else
{
ev.HasMorePages = true;
}
}
 
P

Peter Duniho

I'm having trouble implementing printing of a DataGridView. Below is the
PrintPage event. The problem is that it doesn't matter how many pages the
DataGridView content spans, the code below will print the first page
only. I want all pages to be printed.

What am I missing here?

I'm not sure. What I can't figure out from my cursory scan of your code
is why you only get one page printed.

As near as I can tell, the code you posted should print the first page
over and over again.

Do you have any reason to believe that you would ever see "count" be equal
to or greater than the row count, and thus set the HasMorePages property
to false? If you put a breakpoint on the line setting HasMorePages to
false, does that breakpoint ever get hit?

I admit, it's been a long day and I might be overlooking something. But
it seems to me that you never set HasMorePages to false, causing the print
job to go on forever,_and_ you always restart your "count" variable at 0,
causing the same data to be printed on each page.

Neither of those things seem right to me. :)

Pete
 
J

Jeff

I replaced the local variable count with a class variable (property), and
each time a print job is started that variable is set to 0...

Yes ev.HasMorePages get set to false after the while loop, I've set a
breakpoint there in the code and that breakpoint get triggered each time I
run this code (this happend also before I replaced the local count variable
with a class variable). I'm wondering if this may be caused by the
linesPerPage variable.. I'm debugging now to see if linesPerPage may cause
this problem
 
C

ClayB

You might try this.

Keep your local count variable as you have it in your previous code.
But add a form level variable firstRowIndexToPrint and set it to be
zero in the BeginPrint (so it is zero when the printing starts).

Then instead of

int count = 0;
use

int count = this.firstRowIndexToPrint;

And after the printing loop before you exit print_PrintPage, add a
line such as

this.firstRowIndexToPrint = count;

so firstRowIndexToPrint will be set properly for the next call into
print_PrintPage that prints the next page.

===================
Clay Burch
Syncfusion, Inc.
 
J

Jeff

I've fixed the problem. The problem was in linesPerPage as it did have more
lines than the paper has space for...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top