Click here to Skip to main content
11,935,012 members (52,582 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C#
Hi guys

I was wondering if some one can help me. I need to print a Datagridview with coloured cells and cells that have vertical text. I can already print the gridview but my problem is that it shows the vertical text horizontal and the coloured cells shows white. here is my code :

private void BtnPrint_Click(object sender, EventArgs e)
    //Open the print dialog
    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = thePrintDocument;
    printDialog.UseEXDialog = true;
    //Get the document
    if (DialogResult.OK == printDialog.ShowDialog())
        thePrintDocument.DocumentName = "Test Page Print";
    Note: In case you want to show the Print Preview Dialog instead of
    Print Dialog then comment the above code and uncomment the following code
    //Open the print preview dialog
    PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
    objPPdialog.Document = thePrintDocument;
public void thePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //Set the left margin
        int iLeftMargin = e.MarginBounds.Left;
        //Set the top margin
        int iTopMargin = e.MarginBounds.Top;
        //Whether more pages have to print or not
        bool bMorePagesToPrint = false;
        int iTmpWidth = 0;
        //For the first page to print set the cell width and header height
        if (bFirstPage)
            foreach (DataGridViewColumn GridCol in this.DGVReport.Columns)
                iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                               (double)iTotalWidth * (double)iTotalWidth *
                               ((double)e.MarginBounds.Width / (double)iTotalWidth))));
                iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                            GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
                // Save width and height of headres
                iLeftMargin += iTmpWidth;
        //Loop till all the grid rows not get printed
        while (iRow <= DGVReport.Rows.Count - 1)
            DataGridViewRow GridRow = this.DGVReport.Rows[iRow];
            //Set the cell height
            iCellHeight = GridRow.Height + 5;
            int iCount = 0;
            //Check whether the current page settings allow more rows to print
            if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
                bNewPage = true;
                bFirstPage = false;
                bMorePagesToPrint = true;
                if (bNewPage)
                    //Draw Header
                    e.Graphics.DrawString("Report", new Font(this.DGVReport.Font, FontStyle.Bold),
                            Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
                            e.Graphics.MeasureString("Report", new Font(this.DGVReport.Font,
                            FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                    String strDate = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
                    //Draw Date
                    e.Graphics.DrawString(strDate, new Font(this.DGVReport.Font, FontStyle.Bold),
                            Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
                            e.Graphics.MeasureString(strDate, new Font(DGVReport.Font,
                            FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top -
                            e.Graphics.MeasureString("Report", new Font(new Font(this.DGVReport.Font,
                            FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
                    //Draw Columns
                    iTopMargin = e.MarginBounds.Top;
                    foreach (DataGridViewColumn GridCol in this.DGVReport.Columns)
                        e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                            new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                            (int)arrColumnWidths[iCount], iHeaderHeight));
                            new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
                            (int)arrColumnWidths[iCount], iHeaderHeight));
                        e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                            new SolidBrush(GridCol.InheritedStyle.ForeColor),
                            new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
                            (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
                    bNewPage = false;
                    iTopMargin += iHeaderHeight;
                iCount = 0;
                //Draw Columns Contents
                foreach (DataGridViewCell Cel in GridRow.Cells)
                    if (Cel.Value != null)
                        e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
                                    new SolidBrush(Cel.InheritedStyle.ForeColor),
                                    new RectangleF((int)arrColumnLefts[iCount], (float)iTopMargin,
                                    (int)arrColumnWidths[iCount], (float)iCellHeight), strFormat);
                    //Drawing Cells Borders
                    e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],
                            iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
            iTopMargin += iCellHeight;
        //If more lines exist, print another page.
        if (bMorePagesToPrint)
            e.HasMorePages = true;
            e.HasMorePages = false;
    catch (Exception exc)
        MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted 26-Jun-11 21:33pm
Edited 26-Jun-11 21:35pm

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 27 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100