Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
{
    PrintDocument prnt = new PrintDocument();
    PrintPreviewDialog printD = new PrintPreviewDialog();

    printD.Document = prnt;
    prnt.PrintPage += prnt_PrintPage;
    prnt.DefaultPageSettings.PaperSize = new PaperSize("short", 850, 1100);
    prnt.DefaultPageSettings.Landscape = false;
    printD.Show();
}

void prnt_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Graphics grap = e.Graphics;
    int y = 210;

    for (int o = line; o < dataGridView2.RowCount - 1; o++)
    {
        grap.DrawString("BluRe's Pet Store", new Font("Verdana", 18), Brushes.Black, new Point(300, 50));
        grap.DrawString("Cats Information", new Font("Verdana", 18), Brushes.Black, new Point(300, 80));

        grap.DrawString("Picture", new Font("arial", 14), Brushes.Teal, new Point(50, 150));
        grap.DrawString("Name", new Font("arial", 14), Brushes.Teal, new Point(230, 150));
        grap.DrawString("Breed", new Font("arial", 14), Brushes.Teal, new Point(400, 150));
        grap.DrawString("Type", new Font("arial", 14), Brushes.Teal, new Point(550, 150));
        grap.DrawString("Information", new Font("arial", 14), Brushes.Teal, new Point(710, 150));

        byte[] gambar = new byte[0];
        gambar = (byte[])(dataGridView2.CurrentRow.Cells[o].Value);//the error occur
        MemoryStream ms = new MemoryStream(gambar);
        Bitmap bm = new Bitmap(ms);
        //e.Graphics.DrawImage(bm, 50, y);

        grap.DrawImage(bm, 40, y);
        grap.DrawString(dataGridView2.Rows[o].Cells["Name"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(260, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Breed"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(430, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Type"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(530, y));
        grap.DrawString(dataGridView2.Rows[o].Cells["Information"].Value.ToString(), new Font("arial", 14), Brushes.Black, new Point(710, y));

        y = y + 40;
        line++;
        if (line % 12 == 0)
        {
            break;
        }
    }

    if (line < dataGridView2.RowCount - 1)
    {
        page = page + 1;
        e.HasMorePages = true;
    }
    else
    {
        line = 0;
        e.HasMorePages = false;
        page = 1;
    } 
}
Posted
Updated 29-Mar-15 23:31pm
v4
Comments
Andreas Gieriet 30-Mar-15 5:08am    
What do you expect from us?
Where do you have your cast? What do you expect a cast should do?
Make it easy to help you...
Regards
Andi

1 solution

What that means is that you have made a big mistake in your DB design: you are storing your images in NVARCHAR or VARCHAR fields: you can't do that and reliably access the data because they are not "absolute" fields - they are character based. You need to store them in a byte based column.

See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows you what to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900