Thursday, 29 December 2011

Printing Gridview in Csharp

This tutorial based on printing bill format from gridview using printpreviewDialog and PrintDocument

1.You need create gridview shown in figure and add values to it.

2.Get output like shown in figure below...




private void button2_Click(object sender, EventArgs e)
{
try
{
System.Drawing.Printing.PrintDocument prDoc = new System.Drawing.Printing.PrintDocument();
System.Windows.Forms.PrintPreviewDialog ppd = new System.Windows.Forms.PrintPreviewDialog();
System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
prDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

//Asign PrintDocument to PrintPreviewDialog
ppd.Document = prDoc;
//Show Print Preview
ppd.ShowDialog();
}
catch(Exception ex) { }
}



private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

try
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
//set the pen's DashStyle to dash - dot
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(pen, new Point(45, 120), new Point(600, 120));
Pen blackPen = new Pen(Color.Black, 1);
//Create array of rectangles.
RectangleF[] rects =
{
new RectangleF( 20.0F, 200.0F, 500.0F, 100.0F),
new RectangleF(550.0F, 200.0F, 250.0F, 45.0F),
new RectangleF(550.0F, 250.0F, 250.0F, 45.0F),
//2nd boxes
new RectangleF(20.0F, 310.0F, 50.0F, 45.0F),
new RectangleF(80.0F, 310.0F, 350.0F, 45.0F),
new RectangleF(440.0F, 310.0F, 80.0F, 45.0F),
new RectangleF(550.0F, 310.0F, 80.0F, 45.0F),
new RectangleF(640.0F, 310.0F, 160.0F, 45.0F),


//3d boxes
new RectangleF(20.0F, 360.0F, 50.0F, 400.0F),
new RectangleF(80.0F, 360.0F, 350.0F, 400.0F),
new RectangleF(440.0F, 360.0F, 80.0F, 400.0F),
new RectangleF(550.0F, 360.0F, 80.0F, 400.0F),
new RectangleF(640.0F, 360.0F, 160.0F, 400.0F),


//4th boxes
new RectangleF(20.0F, 780.0F, 500.0F, 45.0F),
new RectangleF(550.0F, 780.0F, 80.0F, 45.0F),
new RectangleF(640.0F, 780.0F, 160.0F, 45.0F)


};


//Draw rectangles to screen.
e.Graphics.DrawRectangles(blackPen, rects);
e.Graphics.DrawLine(new Pen(Brushes.Black),
new Point(550, 720), new Point(630, 720));
e.Graphics.DrawLine(new Pen(Brushes.Black), new Point(550, 740), new Point(630, 740));
e.Graphics.DrawString((int.Parse(txtadvance.Text)+int.Parse(t xt_paid.Text)).ToString(),new Font("Times New Ro man", 12, FontStyle.Bold), Brushes.Black,new Poi ntF(650, 720));
e.Graphics.DrawLine(new Pen(Brushes.Black), new Point(640, 74 0), new Point(800, 740));
e.Graphics.DrawLine(new Pen(Brushes.Black), new Point(640, 72 0), new Point(800, 720));


e.Graphics.DrawString("Sr. No.", new Font("TimesNew Roman", 1 2, FontStyle.Bold),Brushes.Black, new PointF(20, 310));
e.Graphics.DrawString("Product / Size", new Font("Times New R oman", 12, FontStyle.Bold), Brushes.Black, new PointF(170,310));
e.Graphics.DrawString("Sq.Ft", new Font("Times New Roman", 12 , FontStyle.Bold),Brushes.Black, new PointF(450, 310));
e.Graphics.DrawString("Rate", new Font("Times New Roman", 12, FontStyle.Bold),Brushes.Black, new PointF(560, 310));
e.Graphics.DrawString("Total Rate", new Font("Times New Roman ", 12, FontStyle.Bold),Brushes.Black, new PointF(670, 310));
e.Graphics.DrawString("Bill/Chalan No. " + txt_crbillno.Text, new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Blac k,new PointF(560,200));
e.Graphics.DrawString("Date. " + DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Ye ar,new Font("Times New Roman", 12, FontStyle.Bold) , Brushes.Black,new PointF(560, 250));
e.Graphics.DrawString("Total", new Font("Times New Roman", 12 , FontStyle.Bold), Brushes.Black, new PointF(550, 740));


e.Graphics.DrawString("Balance", new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, new PointF(550, 780));
e.Graphics.DrawString("Advance", new Font("Times New Roman", 12, FontStyle.Bold),Brushes.Black, new PointF(550, 720));


//grid view entries Starts here
int x = 140; //X points


int y = 340; //y poiints


int sr = 1; //Serial number e.Graphics.DrawString(txt_total.Text, new Font("Times New Rom an", 12, FontStyle.Bold),Brushes.Black, new PointF(650, 740));
e.Graphics.DrawString(txt_balance.Text, new Font("Times New R oman", 12, FontStyle.Bold),Brushes.Black, new PointF(650, 780));


for(int i = 0; i <= this.dataGridViewp.RowCount - 2; i++)
{
y = y + 30; //increment height
e.Graphics.DrawString(sr.ToString(), new Font("Times New Rom an", 10), Brushes.Black, new PointF(25,y));


string productsize = dataGridViewp.Rows[i].Cells[1].Value.To String() + " " + dataGridViewp.Rows[i].Cells[2].Value.ToString();


e.Graphics.DrawString(productsize, new Font("Times New Roman", 10 ), Brushes.Black, new PointF(90,y));


string sqf = dataGridViewp.Rows[i].Cells[3].Value.ToString();


e.Graphics.DrawString(sqf, new Font("Times New Roman", 10), Brus hes.Black, new PointF(460, y));


string rate = dataGridViewp.Rows[i].Cells[4].Value.ToString();


e.Graphics.DrawString(rate,new Font("Times New Roman", 10), Brus hes.Black, new PointF(570, y));


string nrate = dataGridViewp.Rows[i].Cells[5].Value.ToString();
e.Graphics.DrawString(nrate, new Font("Times New Roman", 10), B rushes.Black, new PointF(660,y));
sr++;
}
catch (Exception ex) { }
}

No comments:

Post a Comment