How can I send GridView to 开发者_高级运维Printer in C#
A DataGridView is a Control. There is no Print function available for it to my knowledge.
You'll need to take the data and format it in a Report (using something like Crystal Reports or Microsoft Reporting Services).
EDIT: Here's a bit more information about how to do it: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/dc9d3acf-ccf8-457f-ba31-ef09fb357aee
You can do this using a combination of the PrintDocument
class and your DataGridView
's DrawToBitmap(...)
method:
using System.Drawing.Printing;
private void Form1_Load(object sender, EventArgs e)
{
PrintDocument printer = new PrintDocument();
printer.PrintPage += printer_PrintPage;
printer.Print();
}
void printer_PrintPage(object sender, PrintPageEventArgs e)
{
using (Bitmap bmp = new Bitmap(dataGridView1.Width,
dataGridView1.Height))
{
dataGridView1.DrawToBitmap(bmp,
new Rectangle(0, 0, bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp, 0, 0);
}
e.HasMorePages = false;
}
This may not be exactly what you need, however, since this will print the DataGridView
exactly as it looks on your form (i.e. with scrollbars visible and much of your data not visible).
精彩评论