Click here to Skip to main content
14,872,121 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello

I want to print data grid view. I have no idea how to do it.

Please some body help me.
Posted

Your best bet is always to format and print the data from your data source, not trying to print a control.
   
Hej Mate

I hope that I understand your needs on the printing concern. I have found a link which I hope will be helpful for your task:
http://www.xmlfox.com/print_datagridview.htm

If this link is not functioning, try to maintain the code up on your requirement.
C#
private DataGridViewPrint PrintGrid;
private void btnPrint_Click(object sender, System.EventArgs e)
{
frmPrint fpr = new frmPrint();
fpr.Title = DataGridView1.CaptionText;
fpr.ShowDialog();
if (fpr.Result > 0)
{
PrintGrid = new DataGridViewPrint(printDocument1, DataGridView1, fpr.bBlackWhite);
PrintGrid.PrintTitle = fpr.bTitle;
PrintGrid.Title = fpr.Title;
if (fpr.Result == 1) // Print
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
// The Print method prints the DataGridView without using a print dialog.
// Use a PrintDialog when you want to offer the user the ability to choose print settings.
printDocument1.Print();
}
}
else if (fpr.Result == 2) // Page setup
{
pageSetupDialog1.ShowDialog();
}
else if (fpr.Result == 3) // Preview
{
printPreviewDialog1.Icon = fpr.Icon;
printPreviewDialog1.ShowDialog();
}
}
}
// Specify the output to print by handling the PrintPage event 
// and by using the Graphics included in the PrintPageEventArgs.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Print method of DataGridViewPrint class starts the custom DataGridView's printing process.
e.HasMorePages = PrintGrid.Print(e.Graphics);
}

good luck
   
v2
In the past, I remember using a code that would transform a DataGridView into a HTML file that could then be embedded in WebBrowser control and printed. It worked flawlessy, though I can't find it anymore. Instead, try using this Another DataGridView Printer[^].
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900