Print all data in datagridview including image using printdocument in c#.

BgnerNprg207 226 Reputation points
2022-12-31T00:10:29.143+00:00

275060-untitled.png

Good day,

I want to print that data from datagridview using printdocument without using any connection string connect to database.

I am using printdocument to print text, and here's my code.

e.Graphics.DrawString(dtgREPORT.Rows[l].Cells1.FormattedValue.ToString(), dtgREPORT.Font = new Font("Arial", 10), Brushes.Black, new RectangleF(0, height, dtgREPORT.Columns[2].Width, dtgREPORT.Rows[0].Height));//stock numbr

and I tried to create for the image, but it's error because of
FormattedValue.ToString()

and here's my code.
e.Graphics.DrawImage(dtgREPORT.Rows[l].Cells[2].FormattedValue.ToString(), dtgREPORT.Font = new Font("Arial", 10), Brushes.Black, new RectangleF(125, height, dtgREPORT.Columns[2].Width, dtgREPORT.Rows[0].Height));//image

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,682 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2023-01-02T06:48:37.387+00:00

    @BgnerNprg207 , Welcome to Microsoft Q&A, you could try to convert your cell value to bitmap then you could print it successfully.

    Here is a code example you could refer to.

           private void Form1_Load(object sender, EventArgs e)  
        {  
            DataTable dt = new DataTable();  
            dt.Columns.Add("Image", typeof(byte[]));  
            dt.Columns.Add("Id", typeof(int));  
            dt.Columns.Add("Name", typeof(string));  
            Image im = Image.FromFile("C:\\Users\\Administrator\\Desktop\\cat.jfif");  
            var arr = ImageToByteArray(im);  
            dt.Rows.Add(arr, 1001, "test1");  
            dataGridView1.DataSource = dt;  
        }  
        public byte[] ImageToByteArray(System.Drawing.Image imageIn)  
        {  
            using (var ms = new MemoryStream())  
            {  
                imageIn.Save(ms, imageIn.RawFormat);  
                return ms.ToArray();  
            }  
        }  
        public Image byteArrayToImage(byte[] byteArrayIn)  
        {  
            MemoryStream ms = new MemoryStream(byteArrayIn);  
            Image returnImage = Image.FromStream(ms);  
            return returnImage;  
        }  
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)  
        {  
            byte[] arr = (byte[])dataGridView1.Rows[0].Cells[0].Value;  
            var image = byteArrayToImage(arr);  
            e.Graphics.DrawImage(image, 620, 55, 120, 120);  
        }  
        private void button1_Click(object sender, EventArgs e)  
        {  
            printDocument1.Print();  
        }  
    

    Result in pdf:

    275258-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful