Print only DataGridView value in using C# in Windows Application

Last Reply one month ago By dharmendr

Posted one month ago

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());
    }
}

 

Posted one month ago

Hi .NetCoder,

I will get back to you soon.


Posted one month ago

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());
        }
    }
}