Hi .NetCoder,
You need to loop through the DataGridView rows and add each row on by one.
Refer below example.
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindGrid();
}
private void BindGrid()
{
string constring = @"Data Source=.;Initial Catalog=Test;User id = sa;password=pass@123";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId Id,Name,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
grdData.DataSource = ds.Tables[0];
}
}
}
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Landscape = true;
printDialog.Document = printDocument;
printDocument.DocumentName = "QuotationMaster.pdf";
printDocument.PrintPage += new PrintPageEventHandler(CreateReceiptA4);
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)
{
try
{
Graphics graphic = e.Graphics;
Font font = new Font("Courier New", 8);
Font font2 = new Font("Courier New", 10, FontStyle.Bold);
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;
graphic.DrawString("Test", new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY);
string Data2 = "";
Data2 = " Label Man".PadLeft(10).PadRight(100);
if (5 > 0)
{
Data2 += " Vat Reg No : " + "123456";
}
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 = "Id".PadRight(10) + "Name".PadRight(10) + "Country".PadRight(10);
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
for (int i = 0; i < grdData.Rows.Count - 1; i++)
{
string id = grdData.Rows[i].Cells[0].Value.ToString();
string name = grdData.Rows[i].Cells[1].Value.ToString();
string country = grdData.Rows[i].Cells[2].Value.ToString();
//create the string to print on the reciept
string data = id + "\t";
data += name + "\t";
data += country + "\t";
graphic.DrawString(data, font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight + 2;
offset = offset + (int)fontHeight + 2;
}
offset = offset + (int)fontHeight;
graphic.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.ToString());
}
}
}