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)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = thePrintDocument;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
thePrintDocument.DocumentName = "Test Page Print";
thePrintDocument.Print();
}
PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
objPPdialog.Document = thePrintDocument;
objPPdialog.ShowDialog();
}
public void thePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
int iLeftMargin = e.MarginBounds.Left;
int iTopMargin = e.MarginBounds.Top;
bool bMorePagesToPrint = false;
int iTmpWidth = 0;
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;
arrColumnLefts.Add(iLeftMargin);
arrColumnWidths.Add(iTmpWidth);
iLeftMargin += iTmpWidth;
}
}
while (iRow <= DGVReport.Rows.Count - 1)
{
DataGridViewRow GridRow = this.DGVReport.Rows[iRow];
iCellHeight = GridRow.Height + 5;
int iCount = 0;
if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
bNewPage = true;
bFirstPage = false;
bMorePagesToPrint = true;
break;
}
else
{
if (bNewPage)
{
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();
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);
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;
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);
}
e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],
iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
iCount++;
}
}
iRow++;
iTopMargin += iCellHeight;
}
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]