Lets say i have a table with 20 rows, and I want to print 5 employee names on each page. Total 4 pages.
The problem with my current code is, that it tries to display every employee name on the first page. Additionally it ends in a infinite loop. This is my code:
DataTable dt = new DataTable();
private void FillDataTable()
{
SqlConnection sqlConnection = GetConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string query =
@"SELECT * FROM EMPLOYEES ORDER BY ID";
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
dt.Load(reader);
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
sqlConnection.Close();
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
foreach(DataRow row in dt.Rows)
{
e.Graphics.DrawString(row[4].ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, y, new StringFormat());
y += 100;
if (itemperpage < 5)
{
itemperpage += 1;
e.HasMorePages = false;
}
else
{
e.HasMorePages = true;
}
}
}
private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
y = 80;
itemperpage = 0;
}
What am I missing here? How to display only 5 employees names on each page until the SQL result table is done?