I have the following code to print a report from a command button:
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
Font _normalFont;
_normalFont = new System.Drawing.Font("Courier", 10, FontStyle.Regular);
int yPosition = e.MarginBounds.Bottom - 10;
int xPosition = e.MarginBounds.Right - 150;
string toPrint = string.Format("Printed on {0:dd-MMM-yyyy}", DateTime.Now);
e.Graphics.DrawString(toPrint, _normalFont, new SolidBrush(Color.Black), xPosition, yPosition);
e.HasMorePages = false;
string sqlText = "Select * From Contacts order by [LastName],[FirstName]";
m_daDataAdapter = new OleDbDataAdapter(sqlText, m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
DataSet dtSet = new DataSet();
m_daDataAdapter.Fill(dtSet, "Contacts");
int countLine = 0;
int x = 0;
int y = 0;
for (int i = countLine; i < dtSet.Tables[0].Rows.Count; i++)
{
DataRow dt = dtSet.Tables[0].Rows;
e.Graphics.DrawString(dt["LastName"].ToString() + ", " + dt["FirstName"].ToString() + ", " + dt["HPhone"].ToString(), _normalFont, Brushes.Black, x, y);
y = y + 40;
if (y > e.MarginBounds.Bottom)
{
countLine = i+1;
e.HasMorePages = true;
break;
}
}
}
It created the print preview except it goes into a loop creating an infinite number of pages but only showing the information on the first page.
Any ideas what needs to be changed?
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
Font _normalFont;
_normalFont = new System.Drawing.Font("Courier", 10, FontStyle.Regular);
int yPosition = e.MarginBounds.Bottom - 10;
int xPosition = e.MarginBounds.Right - 150;
string toPrint = string.Format("Printed on {0:dd-MMM-yyyy}", DateTime.Now);
e.Graphics.DrawString(toPrint, _normalFont, new SolidBrush(Color.Black), xPosition, yPosition);
e.HasMorePages = false;
string sqlText = "Select * From Contacts order by [LastName],[FirstName]";
m_daDataAdapter = new OleDbDataAdapter(sqlText, m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtContacts);
DataSet dtSet = new DataSet();
m_daDataAdapter.Fill(dtSet, "Contacts");
int countLine = 0;
int x = 0;
int y = 0;
for (int i = countLine; i < dtSet.Tables[0].Rows.Count; i++)
{
DataRow dt = dtSet.Tables[0].Rows;
e.Graphics.DrawString(dt["LastName"].ToString() + ", " + dt["FirstName"].ToString() + ", " + dt["HPhone"].ToString(), _normalFont, Brushes.Black, x, y);
y = y + 40;
if (y > e.MarginBounds.Bottom)
{
countLine = i+1;
e.HasMorePages = true;
break;
}
}
}
It created the print preview except it goes into a loop creating an infinite number of pages but only showing the information on the first page.
Any ideas what needs to be changed?