Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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";
        thePrintDocument.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;
    objPPdialog.ShowDialog();
}

public void thePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    try
    {
        //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
                arrColumnLefts.Add(iLeftMargin);
                arrColumnWidths.Add(iTmpWidth);
                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;
                break;
            }
            else
            {
                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));

                        e.Graphics.DrawRectangle(Pens.Black,
                            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);
                        iCount++;
                    }
                    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));

                    iCount++;
                }
            }
            iRow++;
            iTopMargin += iCellHeight;
        }

        //If more lines exist, print another page.
        if (bMorePagesToPrint)
            e.HasMorePages = true;
        else
            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
Updated 26-Jun-11 20:35pm
v2

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