tags:

views:

295

answers:

3

How can I send GridView to Printer in C#

+2  A: 

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

Michael Todd
+1  A: 

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).

MusiGenesis
A: 

Hi nobugz i have question in usage field i can create printer but when i call PrintPage() methode i don't know anything about System.Drawing.Printing.PrintPageEventArgs e please explain more about this an show me how can i use thx alot

MOhamamd