Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
Hello Respected members
I have problam
How to write code for print datagridview with print preview option in c# in window application
 
I have code for print but its directly print document not Previewing document that why the i am not able to print all datagridview item
 
Can any body help me for sort out my problam
 

 
private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               printDocument1.Print();
               
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
 
       }
 
       private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
       {
           Bitmap dataGridViewImage=new Bitmap(this.dataGridView1.Width,this.dataGridView1.Height);
           dataGridView1.DrawToBitmap(dataGridViewImage, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
           e.Graphics.DrawImage(dataGridViewImage, 0, 0);
           
 
       }
 
Thans in Advance
 
Good luck Smile | :)
Posted 28-Mar-13 2:32am
Comments
onurag19 at 28-Mar-13 7:35am
   
i think you should use print preview dialog box.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

// make a function named zpt
 
int rw=dataGridView1.Rows.Count; //define rw as globly variable in form
public void zpt()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
Font font = new Font("Arial", 10);
PaperSize psz = new PaperSize("Custom", 100, 200);
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psz;
pdoc.DefaultPageSettings.PaperSize.Height = 820;
pdoc.DefaultPageSettings.PaperSize.Width = 700;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult res = pd.ShowDialog();
if (res == DialogResult.OK)
{
PrintPreviewDialog prv = new PrintPreviewDialog();
prv.Document = pdoc;
res = prv.ShowDialog();
if (res == DialogResult.OK)
{
pdoc.Print();
}
}
 
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 50;
int startY = 65;
int Offset = 40;
graphics.DrawString("Welcome to Bakery Shop", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
string underLine = "------------------------------------------";
graphics.DrawString(underLine, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
int a = dataGridView1.Rows.Count;
for (int i = 0; i < a; i++)
{
graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
graphics.DrawString("\t"+Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
}
}
 

 
private void button1_Click(object sender, EventArgs e)
{

dataGridView1.Rows.Add();
dataGridView1.Rows[rw].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[rw].Cells[1].Value = textBox2.Text;
rw++;
}
 
private void button2_Click(object sender, EventArgs e)
{
// on print Button which is in your window for code this...
zpt();
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 12 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100