Question print datagridview in pos format

Uncle OBK

Member
Joined
Sep 17, 2019
Messages
5
Programming Experience
1-3
please how do i get the data from a datagridview so i can print out a receipt in pos format: like a pos receipt
 
What have you tried, thought of or attempted?

If you search around, you'll find this question has been asked a number of times before.

Also appears to be cross-posted here on code project
 
Last edited:
i have tried a few code i was given but could not get the data from the gridview plus the ones i found here didn't suit my need but a link will be appreciated.
my challenge is the paint and drawing tools from c#, don't know much about it as i'm still a learning about it.
 
Show us your current code.

As an aside, it's usually easier (and also cuts down on the paper wastage and/or extra clicks when printing to PDF), to just create a simple window and do your drawing to that window first to figure out the logistics and formatting. If you can draw to a window, then drawing to the printer is essentially the same although the dimensions and pixel sizes maybe different.

As an aside, have you even gone through some of the sample code provided in MSDN? For example, there is this in the PrintPage event documentation.
 
Further to Skydiver's suggestion, you can use the PrintPreviewDialog to display what would be printed using a PrintDocument wityhout actually printing it, so you can use basically the same code as for a real print run.

To answer the question as asked, you would get the data from the grid to print in the same way as you would get it for anything else. Getting data from a grid has absolutely nothing to do with printing so you can do it independently. So, forget the printing first and just write code to get the data you need. You can use the debugger to test that. Once you can do that, then you can think about printing. You can practice how to print completely independently of your grid, so do that. Once you understand how to get the data and how to print, only then should you consider putting the two together. This is how learning and problem solving in general works: divide and conquer.
 
pos.jpg

this is what i'm trying to achieve. Picked it from stackoverflow.
I'm a novice when it comes to windows paint and drawing. Please i need help
 
Regardless if you are a novice or not, show us your current code. We are not a code writing service. If you're expectation is for us to write the code for you, then you'll likely be waiting for a very long time.
 
We know you need help, that's why you're here. But nobody will write it for you. That's your job. Given you've received pointers above, start there and once you have something to show as an attempt, post it up if it doesn't work as expected and explain your problem with the code you wrote. Only then will receive further help. I should let you know, begging for code is frowned upon.

Try looking up on MSDN the inline code in John's post and the link in Skydivers post and see if they are what you're looking for to get started.
 
private void print()
{
string welcome = "Thank You For Shopping with us";
string InvoiceNo = txtInvoiceNo.Text;
decimal gross = Convert.ToInt32(txtGross.Text);
decimal net = Convert.ToInt32(txtNet.Text);
decimal discount = gross - net;
string InvoiceDate = dateTimePicker1.Value.ToLongDateString();

int lineHeight = 20;
int supplementaryLines = 15;

Bitmap bitm = new Bitmap(welcome.Length * 30, (supplementaryLines + dataGridView1.Rows.Count) * lineHeight);
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
using (Graphics graphic = Graphics.FromImage(bitm))
{
int startX = 0;
int startY = 0;
int offsetY = 0;
Font newfont2 = null;
Font itemFont = null;
SolidBrush black = null;
SolidBrush white = null;

try
{
//Font newfont = new Font("Arial Black", 8);
newfont2 = new Font("Calibri", 11);
itemFont = new Font("Calibri", 11);

black = new SolidBrush(Color.Black);
white = new SolidBrush(Color.White);

//PointF point = new PointF(40f, 2f);

graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
graphic.DrawString("" + InvoiceNo + "invoice no.", newfont2, black, startX + 150, startY + offsetY);
offsetY = offsetY + lineHeight;

//PointF pointPrice = new PointF(15f, 45f);
graphic.DrawString("" + InvoiceDate + "", newfont2, black, startX, startY + offsetY);
offsetY = offsetY + lineHeight;
offsetY = offsetY + lineHeight;

graphic.DrawString("Product Name " + "Product ID " + "Description", newfont2, black, startX + 15, startY + offsetY);
offsetY = offsetY + lineHeight;
offsetY = offsetY + lineHeight;
graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
//PointF pointPname = new PointF(10f, 65f);
//PointF pointBar = new PointF(10f, 65f);

offsetY = offsetY + lineHeight;
offsetY = offsetY + lineHeight;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int ii = 1;
ii++;

graphic.DrawString(" " + dataGridView1.Rows.Cells[3].Value + " " + dataGridView1.Rows.Cells[2].Value + " " + dataGridView1.Rows.Cells[1].Value + "", itemFont,
black, startX + 15, startY + offsetY);
offsetY = offsetY + lineHeight;
}
offsetY = offsetY + lineHeight;
graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
offsetY = offsetY + lineHeight;
graphic.DrawString("Gross :" + gross + "", newfont2, black, startX + 15, startY + offsetY);
offsetY = offsetY + lineHeight;
graphic.DrawString("Discount :" + discount + "", newfont2, black, startX + 15, startY + offsetY);
offsetY = offsetY + lineHeight;
graphic.DrawString("Net :" + net + "", newfont2, black, startX + 15, startY + offsetY);
offsetY = offsetY + lineHeight;
offsetY = offsetY + lineHeight;
graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
offsetY = offsetY + lineHeight;
graphic.DrawString("" + welcome + "", newfont2, black, startX, startY + offsetY);
offsetY = offsetY + lineHeight;
}
finally
{
black.Dispose();
white.Dispose();
itemFont.Dispose();
newfont2.Dispose();
}
}

using (MemoryStream Mmst = new MemoryStream())
{
bitm.Save("ms", ImageFormat.Jpeg);
pictureBox1.Image = bitm;
pictureBox1.Width = bitm.Width;
pictureBox1.Height = bitm.Height;


}


}


this is the code i have bn trying to work around
 
C#:
private void printready()
        {
            string welcome = "Thank You For Shopping with us";
            string InvoiceNo = txtInvoiceNo.Text;
            decimal gross = Convert.ToInt32(txtGross.Text);
            decimal net = Convert.ToInt32(txtNet.Text);
            decimal discount = gross - net;
            string InvoiceDate = dateTimePicker1.Value.ToLongDateString();

            int lineHeight = 20;
            int supplementaryLines = 15;

            Bitmap bitm = new Bitmap(welcome.Length * 30, (supplementaryLines + dataGridView1.Rows.Count) * lineHeight);
            StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
            using (Graphics graphic = Graphics.FromImage(bitm))
            {
                int startX = 0;
                int startY = 0;
                int offsetY = 0;
                Font newfont2 = null;
                Font itemFont = null;
                SolidBrush black = null;
                SolidBrush white = null;

                try
                {
                    //Font newfont = new Font("Arial Black", 8);
                    newfont2 = new Font("Calibri", 11);
                    itemFont = new Font("Calibri", 11);

                    black = new SolidBrush(Color.Black);
                    white = new SolidBrush(Color.White);

                    //PointF point = new PointF(40f, 2f);

                    graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                    graphic.DrawString("" + InvoiceNo + "invoice no.", newfont2, black, startX + 150, startY + offsetY);
                    offsetY = offsetY + lineHeight;

                    //PointF pointPrice = new PointF(15f, 45f);
                    graphic.DrawString("" + InvoiceDate + "", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;

                    graphic.DrawString("Product Name             " + "Product ID      " + "Description", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    //PointF pointPname = new PointF(10f, 65f);
                    //PointF pointBar = new PointF(10f, 65f);

                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;

                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        int ii = 1;
                        ii++;

                        graphic.DrawString(" " + dataGridView1.Rows[i].Cells[3].Value + "  " + dataGridView1.Rows[i].Cells[2].Value + "  " + dataGridView1.Rows[i].Cells[1].Value + "", itemFont,
                                 black, startX + 15, startY + offsetY);
                        offsetY = offsetY + lineHeight;
                    }
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("Gross :" + gross + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("Discount :" + discount + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("Net :" + net + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("" + welcome + "", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                }
                finally
                {
                    black.Dispose();
                    white.Dispose();
                    itemFont.Dispose();
                    newfont2.Dispose();
                }
            }

            using (MemoryStream Mmst = new MemoryStream())
            {
                bitm.Save("ms", ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;


            }


        }
 
Okay. First question: Why are you drawing to a bitmap?
 
Back
Top Bottom