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;
}
}
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;
}
}