سلام خسته نباشید
توی رویداد pdoc_PrintPage دستورات رو به اینصورت تغیید بدید:
void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
string Header = "بانک اطلاعات اشخاص";
int FontSize = 20;
int StartX = 0, x = 0
, y = 50;
for (int i = this.dataGridView1.Columns.Count - 1; i > -1; i--)
switch (i)
{
case 1:
case 2:
StartX += this.dataGridView1.Columns[i].Width;
break;
}
StartX = e.PageBounds.Width / 2 - StartX / 2;
x = StartX;
System.Drawing.Font HeaderFont =new System.Drawing.Font("Arial", FontSize, FontStyle.Bold,GraphicsUnit.Point);
SizeF Headersize = e.Graphics.MeasureString(Header, HeaderFont);
e.Graphics.DrawString(Header,
HeaderFont, Brushes.Black, new PointF(e.PageBounds.Width / 2 - (Headersize.Width / 2), y));
y += (int)Headersize.Height + 1;
for (int j = this.dataGridView1.Columns.Count - 1; j > -1; j--)
{
switch (j)
{
case 1:
case 2:
Rectangle rect = new Rectangle(x, y,
this.dataGridView1.Columns[j].Width,
this.dataGridView1.ColumnHeadersHeight);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(Brushes.LightGray, rect);
e.Graphics.DrawRectangle(Pens.Black, rect);
if (this.dataGridView1.Columns[j].HeaderText != null)
{
e.Graphics.DrawString(this.dataGridView1.Columns[j].HeaderText,
SystemFonts.DefaultFont, Brushes.Black, rect, sf);
}
x += rect.Width;
break;
}
}
x = StartX;
y += this.dataGridView1.ColumnHeadersHeight;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Cells[0].Value != null)
{
for (int j = this.dataGridView1.Columns.Count - 1; j > -1; j--)
{
switch (j)
{
case 1:
case 2:
DataGridViewCell cell; cell = row.Cells[j];
Rectangle rect = new Rectangle(x, y, cell.Size.Width, cell.Size.Height);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(Pens.Black, rect);
if (cell.Value != null)
{
e.Graphics.DrawString(cell.Value.ToString(),
SystemFonts.DefaultFont, Brushes.Black, rect, sf);
}
x += rect.Width;
break;
}
}
x = StartX;
y += row.Height;
}
}
}
امیدوارم مشکل حل شه