private static void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
short tmpWidth = 0;
short i = 0;
short tmpTop = (short)e.MarginBounds.Top;
short tmpLeft = (short)e.MarginBounds.Left;
try
{
// Before starting first page, it saves Width & Height of Headers and ColumnType
if (PageNo == 1)
{
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible)
{
continue;
}
if (!SelectedColumns.Contains(GridCol.HeaderText))
{
continue;
}
// Detemining whether the columns are fitted to page or not.
if (FitToPageWidth)
{
tmpWidth = (short)(Math.Floor((decimal)GridCol.Width / TotalWidth * TotalWidth * (e.MarginBounds.Width / TotalWidth)));
}
else
{
tmpWidth = (short)GridCol.Width;
}
HeaderHeight = System.Convert.ToInt16(e.Graphics.MeasureString(GridCol.HeaderText, drawfont, tmpWidth).Height + 11);
ColumnLefts.Add(tmpLeft);
ColumnWidths.Add(tmpWidth);
ColumnTypes.Add(GridCol.GetType());
tmpLeft += tmpWidth;
}
}
// Printing Current Page, Row by Row
while (RowPos <= dgv.Rows.Count - 1)
{
DataGridViewRow GridRow = dgv.Rows[RowPos];
if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
{
RowPos++;
goto continueDo;
}
CellHeight = (short)GridRow.Height;
//CellHeight = 30
if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
DrawFooter(e, RowsPerPage);
NewPage = true;
PageNo++;
e.HasMorePages = true;
return;
}
else
{
if (NewPage)
{
// Draw Header
e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(PrintTitle, new Font(dgv.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13);
string s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
e.Graphics.DrawString(s, new Font(dgv.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(s, new Font(dgv.Font, FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top - e.Graphics.MeasureString(PrintTitle, new Font(new Font(dgv.Font, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
// Draw Columns
tmpTop = (short)e.MarginBounds.Top;
i = (short)0;
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible)
{
continue;
}
if (!SelectedColumns.Contains(GridCol.HeaderText))
{
continue;
}
// Grey shading for Header Text Box
e.Graphics.FillRectangle(new SolidBrush(System.Drawing.Color.LightGray), new Rectangle(System.Convert.ToInt32(ColumnLefts[i]), tmpTop, ColumnWidths[i], HeaderHeight));
//Draw Header Text Box
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(System.Convert.ToInt32(ColumnLefts[i]), tmpTop, System.Convert.ToInt32(ColumnWidths[i]), HeaderHeight));
//Draw Header Text
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, new SolidBrush(GridCol.InheritedStyle.ForeColor), new RectangleF(System.Convert.ToSingle(ColumnLefts[i]), tmpTop, System.Convert.ToSingle(
ColumnWidths[i]), HeaderHeight), StrFormat);
i++;
}
NewPage = false;
tmpTop += HeaderHeight;
}
i = (short)0;
foreach (DataGridViewCell Cel in GridRow.Cells)
{
if (!Cel.OwningColumn.Visible)
{
continue;
}
if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
{
continue;
}
// For the TextBox Column
if ((System.Type)ColumnTypes[i] == typeof(DataGridViewTextBoxColumn) || (System.Type)ColumnTypes[i] == typeof(DataGridViewLinkColumn))
{
if (Cel.OwningColumn.HeaderText == "Name" || Cel.OwningColumn.HeaderText == "Address")
{
e.Graphics.DrawString(Cel.Value.ToString(), drawfont1, new SolidBrush(Cel.InheritedStyle.ForeColor), new RectangleF(System.Convert.ToSingle(ColumnLefts[i]), tmpTop, System.Convert.ToSingle(ColumnWidths[i]), CellHeight), StrFormat);
}
else
{
e.Graphics.DrawString(Cel.Value.ToString(), drawfont,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF(System.Convert.ToSingle(ColumnLefts[i]), tmpTop, System.Convert.ToSingle(ColumnWidths[i]),
CellHeight), StrFormat);
}
// For the Button Column
}
else if ((System.Type)ColumnTypes[i] == typeof(DataGridViewButtonColumn))
{
CellButton.Text = Cel.Value.ToString();
CellButton.Size = new Size(System.Convert.ToInt32(ColumnWidths[i]), CellHeight);
Bitmap bmp = new Bitmap(CellButton.Width, CellButton.Height);
CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp, new Point(System.Convert.ToInt32(ColumnLefts[i]), tmpTop));
// For the CheckBox Column
}
else if ((System.Type)ColumnTypes[i] == typeof(DataGridViewCheckBoxColumn))
{
CellCheckBox.Size = new Size(14, 14);
CellCheckBox.Checked = System.Convert.ToBoolean(Cel.Value);
Bitmap bmp = new Bitmap(System.Convert.ToInt32(ColumnWidths[i]), CellHeight);
Graphics tmpGraphics = Graphics.FromImage(bmp);
tmpGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0,
bmp.Width, bmp.Height));
CellCheckBox.DrawToBitmap(bmp, new Rectangle(System.Convert.ToInt32((bmp.Width - CellCheckBox.Width) / 2), System.Convert.ToInt32((bmp.Height - CellCheckBox.Height) / 2), CellCheckBox.Width, CellCheckBox.Height));
e.Graphics.DrawImage(bmp, new Point(System.Convert.ToInt32(ColumnLefts[i]), tmpTop));
// For the ComboBox Column
}
else if ((System.Type)ColumnTypes[i] == typeof(DataGridViewComboBoxColumn))
{
CellComboBox.Size = new Size(System.Convert.ToInt32(ColumnWidths[i]), CellHeight);
Bitmap bmp = new Bitmap(CellComboBox.Width, CellComboBox.Height);
CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp, new Point(System.Convert.ToInt32(ColumnLefts[i]), tmpTop));
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, new SolidBrush(Cel.InheritedStyle.ForeColor), new RectangleF(System.Convert.ToInt32(ColumnLefts[i]) + 1, tmpTop, System.Convert.ToSingle(System.Convert.ToInt32(ColumnWidths[i]) - 16), CellHeight), StrFormatComboBox);
// For the Image Column
}
else if ((System.Type)ColumnTypes[i] == typeof(DataGridViewImageColumn))
{
Rectangle CelSize = new Rectangle(System.Convert.ToInt32(ColumnLefts[i]), tmpTop, System.Convert.ToInt32(ColumnWidths[i]), CellHeight);
Size ImgSize = ((Image)Cel.FormattedValue).Size;
e.Graphics.DrawImage((Image)Cel.FormattedValue, new Rectangle(System.Convert.ToInt32(ColumnLefts[i]) + System.Convert.ToInt32((CelSize.Width - ImgSize.Width) / 2), tmpTop + System.Convert.ToInt32((CelSize.Height - ImgSize.Height) / 2), ((Image)Cel.FormattedValue).Width, ((Image)Cel.FormattedValue).Height));
}
// Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(System.Convert.ToInt32(ColumnLefts[i]), tmpTop, System.Convert.ToInt32(ColumnWidths[i]), CellHeight));
i++;
}
tmpTop += CellHeight;
}
RowPos++;
// For the first page it calculates Rows per Page
if (PageNo == 1)
{
RowsPerPage++;
}
continueDo:
1.GetHashCode(); //VBConversions note: C# requires an executable line here, so a dummy line was added.
}
if (RowsPerPage == 0)
{
return;
}
// Write Footer (Page Number)
DrawFooter(e, RowsPerPage);
e.HasMorePages = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
In above code build Error :
The best overloaded method match for 'System.Drawing.Rectangle.Rectangle(int, int, int, int)' has some invalid arguments