Print only Datagridview value in c# windows application
I want only values not whole datagridview structure because it is a invoice print.
public void PrintReceipt()
{
try
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Landscape = true;
printDialog.Document = printDocument; //add the document to the dialog box...
iPrint = 0;
printDocument.DocumentName = "QuotationMaster.pdf";
printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceiptA4); //add an event handler that will do the printing
//on a till you will not want to ask the user where to print but this is fine for the test envoironment.
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.PrinterSettings.PrintToFile = true;
printDocument.PrinterSettings.PrintFileName = "QuotationMaster.pdf";
printDocument.Print();
}
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.ToString());
}
}
private void CreateReceiptA4(object sender, PrintPageEventArgs e)
{
//this prints the reciept
try
{
string ToCode = "";
string ToDescription = "";
string ToQty = "";
Graphics graphic = e.Graphics;
Font font = new Font("Courier New", 8); //must use a mono spaced font as the spaces need to line up
Font font2 = new Font("Courier New", 10, FontStyle.Bold); //must use a mono spaced font as the spaces need to line up
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Far;
float fontHeight = font.GetHeight();
int startX = 10;
int startY = 10;
int offset = 40;
//" Grandbrand Investments(Pty) Ltd"
graphic.DrawString(GlobalVariable.GlobalActiveCompany.PadLeft(60), new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY);
//offset = offset + (int)fontHeight; //make the spacing consistent
string Data2 = "";
Data2 = " Label Man".PadLeft(10).PadRight(100);
if (GlobalVariable.GlobalActiveCompanyVATNo.Length > 0)
{
Data2 += " Vat Reg No : " + GlobalVariable.GlobalActiveCompanyVATNo;//.PadLeft(50)
}
graphic.DrawString(Data2, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight; //make the spacing consistent
graphic.DrawString(" Report--> Stock Transfer ".PadLeft(10), new Font("Courier New", 10, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + offset);
graphic.DrawString(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss").PadLeft(120), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight; //make the spacing consistent
graphic.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight; //make the spacing consistent
string HeaderData = "";
HeaderData = " Date".PadRight(10) + "Trm#".PadRight(10) + "Tran#".PadRight(10) + "Code From".PadRight(15);
HeaderData += " Description".PadLeft(10).PadRight(15) + "Qty From".PadLeft(10).PadRight(10) + "Code To".PadLeft(10).PadRight(15);
HeaderData += "Description".PadRight(17) + "Qty To".PadLeft(10).PadRight(15) + "Remarks";
graphic.DrawString(HeaderData, font2, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight; //make the spacing consistent
graphic.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight; //make the spacing consistent
offset = offset + (int)fontHeight;
graphic.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.ToString());
}
}