我有一个数据库表,其中有300多条记录,我想使用PrintDocument/PrintPreview打印它,但问题是只有一页显示在PrintPreview中,其余记录丢失了。下面是我的代码,请帮我。
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int valueYPos=80;
int valueXPos=20
string classId=1;
con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='"
+ classId + "'");
con.ExQuery();
foreach (DataRow item in con.ExQuery().Rows)
{
e.Graphics.DrawString("RollNo" +item[0].ToString(), new Font("Arial", 20,
FontStyle.Bold), Brushes.Black, new Point(valueXPos, valueYPos));
e.Graphics.DrawString("RollNo" +item[1].ToString(), new Font("Arial", 20,
FontStyle.Bold), Brushes.Black, new Point(valueXPos+20, valueYPos));
valueYPos +=20;
}
}
private void button1_Click(object sender, EventArgs e)
{
PrintPreviewDialog pPD = new PrintPreviewDialog();
pPD.Document = printDocument1;
pPD.ShowDialog();
}
我不会尝试在PrintPage事件中执行这样的查询。将查询的执行移动到BeginPrint事件中,并在EndPrint事件中执行清理。使用SqlDataReader,您可以在PrintPage处理程序中引用和迭代它。类似这样的代码,尽管您显然需要处理页眉、页脚、页边距、列等。如果用户单击PrintPreviewDialog中的打印按钮,此代码也将正确地重新执行查询。
private SqlConnection conn;
private SqlCommand cmd;
private SqlDataReader reader;
private void button1_Click(object sender, EventArgs e)
{
PrintDocument pDoc = new PrintDocument();
pDoc.BeginPrint += new PrintEventHandler(this.BeginPrint);
pDoc.EndPrint += new PrintEventHandler(this.EndPrint);
pDoc.PrintPage += new PrintPageEventHandler(this.PrintPage);
PrintPreviewDialog pPD = new PrintPreviewDialog();
pPD.Document = pDoc;
pPD.ShowDialog();
}
private void BeginPrint(object sender, PrintEventArgs e)
{
const string sql = "SELECT [Name] FROM [StudentRegistration]";
SqlConnection conn = new SqlConnection(ConnStr);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
conn.Open();
reader = cmd.ExecuteReader(CommandBehavior.SingleResult);
}
private void EndPrint(object sender, PrintEventArgs e)
{
reader.Close();
cmd.Dispose();
conn.Dispose();
}
private void PrintPage(object sender, PrintPageEventArgs e)
{
Font myFont = new Font("Arial", 20, FontStyle.Bold);
int rowHt = myFont.Height();
int valueYPos = 80;
int valueXPos = 20;
bool pageDone = false;
bool moreData = false;
while (!pageDone)
{
moreData = reader.Read();
pageDone = !moreData;
if (moreData)
{
e.Graphics.DrawString(reader.GetString(0), myFont, Brushes.Black, valueXPos, valueYPos);
valueYPos += rowHt;
// Is there room to print another row on this page?
if (valueYPos + rowHt > (e.PageBounds.Height))
pageDone = true;
}
}
e.HasMorePages = moreData;
}
有关打印预览对话框,请参阅以下答案在C#中显示打印预览
首先,你不能访问这样的数据,因为当它试图打印每一页时,它会试图访问数据。
private int pageNo = 1;
private static void Print_Invoice(object sender, PrintPageEventArgs e)
{
int valueYPos=80;
int valueXPos=20
string classId=1;
con.SqlQuery("select [RollNo],[Name] from [dbo].[StudentRegistration] where [ClassId]='"+ classId + "'");
con.ExQuery();
switch (pageNo)
{
case 1:
//Print some Data
e.HasMorePages = true;
break;
case 2:
//Print some Data
e.HasMorePages = true;
break;
case 3:
//Print some Data
e.HasMorePages = false;
break;
default:
e.HasMorePages = false;
break;
}
pageNo++;
}
希望能解决你的问题