我的表单上有一个datagridview控件,我想打印那个datagridview,我使用了下面的代码,但它不起作用
PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new
Point(InvoiceViewergrid.Location.X, InvoiceViewergrid.Location.Y), this.Size));
this.InvokePaint(InvoiceViewergrid, myPaintArgs);
我希望这对你有帮助http://www.codeproject.com/Articles/16670/DataGridView-Printing-by-Selecting-Columns-and-Row
你可以使用这段代码,希望你已经得到了你需要的。
//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("rnrn"+ label14.Text + "rn" + 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++;
}
}