Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Not Working

  • Thread starter Thread starter member 472187
  • Start date Start date
Status
Not open for further replies.
M

member 472187

Guest
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?
 
I solved the problem through trial and error.
 
post the solution to help others.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
My solution minus the page header information is the following:

private int countLine = 0;
private int horizontalPosition = 60;
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
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");
for (int i = countLine; i < dtSet.Tables[0].Rows.Count; i++)
{
DataRow dt = dtSet.Tables[0].Rows;
int lMargin = 20;
string strModified = dt["HPhone"].ToString();
string fName = string.Format(dt["LastName"].ToString() + ", " + dt["FirstName"].ToString());
e.Graphics.DrawString(fName, _normalFont, Brushes.Black, x, y); ;
lMargin = lMargin + 250;
e.Graphics.DrawString(dt["HPhone"].ToString(), _normalFont, Brushes.Black, x, y);
horizontalPosition = horizontalPosition + 25;
if (horizontalPosition + 10 > e.MarginBounds.Bottom)
{
countLine = i + 1;
e.HasMorePages = true;
horizontalPosition = 60;
break;
}
}
}


 
The corrected code above that I posted works correctly except for one flaw that I haven't fixed yet. The print preview I have it open up in works fine but when I then select to print the document only the last page actually prints. I get the same results when I send it to the printer or if I print to a pdf file.

I'm going to look into why any suggestions otherwise would be appreciated.
 
As far as I can tell the following is the corrected code that prints correctly:

private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
Font _normalFont;
_normalFont = new System.Drawing.Font("Arial", 12, FontStyle.Regular);
int a = 20;
int b = 20;
y = 80;
e.Graphics.DrawString("Name", _normalFont, Brushes.Black, a, b);
a = a + 250;
e.Graphics.DrawString("Home Phone", _normalFont, Brushes.Black, a, b);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 20, 45, 720, 45);
int yPosition = e.MarginBounds.Bottom + 20;
int xPosition = e.MarginBounds.Right - 150;
string toPrint2 = string.Format("Printed on {0:dd-MMM-yyyy}", DateTime.Now);
e.Graphics.DrawString(toPrint2, _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");
for (int i = countLine; i < dtSet.Tables[0].Rows.Count; i++)
{
DataRow dt = dtSet.Tables[0].Rows;
int x = 20;
string fName = string.Format(dt["LastName"].ToString() + ", " + dt["FirstName"].ToString());
e.Graphics.DrawString(fName, _normalFont, Brushes.Black, x, y); ;
x = x + 250;
e.Graphics.DrawString(dt["HPhone"].ToString(), _normalFont, Brushes.Black, x, y);
y = y + 25;
e.Graphics.DrawLine(new Pen(Color.Black, 1), 20, y, 720, y);
y = y + 30;
if (y + 10 > e.MarginBounds.Bottom)
{
countLine = i + 1;
e.HasMorePages = true;
y = 80;
break;
}
countLine = 0;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top