while (rdr.Read())
{
if (itemCounter < itemPerPage)
{
e.Graphics.DrawString(rdr["cCode"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(60, Ypos));
e.Graphics.DrawString(rdr["cName"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(120, Ypos));
e.Graphics.DrawString(rdr["cShortAddress"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(310, Ypos));
e.Graphics.DrawString(rdr["cPhone"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(580, Ypos));
Ypos = Ypos + 22;
itemCounter++;
}
else
{
Ypos = 182;
e.HasMorePages = true;
}
}
rdr.Close();
请帮助我,以便我可以控制循环。
我尝试了另一个循环。在for循环中也会出现类似的问题。我想在一页上打印0到14个索引数据,在另一页上打印15到29个索引数据,依此类推。
请了解我是否有任何方法可以通过for循环打印发票。谢谢。
for (i = itenCounter; i < totalItem; i++)
{
rdr.Read();
if (itemCounter < itemPerPage)
{
e.Graphics.DrawString(rdr["cCode"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(60, Ypos));
e.Graphics.DrawString(rdr["cName"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(120, Ypos));
e.Graphics.DrawString(rdr["cShortAddress"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(310, Ypos));
e.Graphics.DrawString(rdr["cPhone"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(580, Ypos));
Ypos = Ypos + 22;
itemCounter++;
}
else
{
Ypos = 182;
e.HasMorePages= true;
}
}
itenCounter = itenCounter + itemPerPage;
以下是使用 Windows 窗体应用程序中的 PrintDocument 从 SQL 数据库打印每页 15 行的完整 C# 代码。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
Button printButton = new Button
{
Text = "Print",
Location = new Point(120, 10)
};
printButton.Click += PrintButton_Click;
Controls.Add(printButton);
}
private PrintDocument printDocument = new PrintDocument();
private SqlDataReader rdr;
private int itemCounter = 0;
private int itemsPerPage = 15;
private int totalItemCount = 40;
private int currentPageIndex = 0;
private void PrintButton_Click(object sender, EventArgs e)
{
StartPrint();
}
private void PrintPage(object sender, PrintPageEventArgs e)
{
if (rdr == null)
{
e.HasMorePages = false;
return;
}
int startIndex = currentPageIndex * itemsPerPage;
int Ypos = 182;
while (itemCounter < startIndex + itemsPerPage && itemCounter < totalItemCount)
{
if (rdr.Read())
{
e.Graphics.DrawString(rdr["cCode"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(60, Ypos));
e.Graphics.DrawString(rdr["cName"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(120, Ypos));
e.Graphics.DrawString(rdr["cShortAddress"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(310, Ypos));
e.Graphics.DrawString(rdr["cPhone"].ToString(), new Font("Arial", 11, FontStyle.Regular), Brushes.Black, new Point(580, Ypos));
Ypos += 22;
itemCounter++;
}
}
if (itemCounter < totalItemCount)
{
e.HasMorePages = true;
currentPageIndex++;
}
else
{
e.HasMorePages = false;
ResetCounters();
}
}
private void ResetCounters()
{
itemCounter = 0;
currentPageIndex = 0;
}
private void StartPrint()
{
using (SqlConnection conn = new SqlConnection("Data Source=(localdb)\\ProjectModels;Initial Catalog=MyData;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT cCode, cName, cShortAddress, cPhone FROM [dbo].[TestTable]", conn))
{
rdr = cmd.ExecuteReader();
printDocument.Print();
}
}
}
}