你可以使用这个代码,希望你在这里得到了你需要的东西。
//Button-Print:
private void button5_Click(object sender, EventArgs e)
{
printPreviewDialog1.ShowDialog();
i = 0;
}
//GlobalValue.
int i = 0;
//printDocument
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("\r\n\r\n"+ label14.Text + "\r\n" + label11.Text + "\t \t \t \t \t \t" + label6.Text, label11.Font, Brushes.Black, 50, 0);
int height = 0;
int width = 0;
Pen p = new Pen(Brushes.Black,2.5f);
//Column1
#region userIDCol
e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
#endregion
//Column2
#region loginTimeCol
e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
#endregion
//Column3
#region logoutTimeCol
e.Graphics.FillRectangle(Brushes.DarkGray, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
#endregion
height = 100;
while (i < dataGridView1.Rows.Count)
{
if(height > e.MarginBounds.Height)
{
height = 100;
e.HasMorePages = true;
return;
}
height += dataGridView1.Rows[0].Height;
//Column1
e.Graphics.DrawRectangle(p, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
//Column2
e.Graphics.DrawRectangle(p, new Rectangle(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
//Column3
e.Graphics.DrawRectangle(p, new Rectangle(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(200 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
i++;
}
}