你好, 我正在开发一个Windows表单应用程序,我想在其中打印datagridview的值。不幸的是我的datagridview包含12列,因此我更改了printdocument的纸张大小。我的Printpreview对话框也显示了良好的结果,但问题是,当我打印此页面时,它不会显示其中的所有列。我必须在单页上打印所有列。 任何人都可以告诉我如何设置打印尺寸。 我的代码在这里:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int height = 0;
int width = 0;
Pen p = new Pen(Brushes.Black, 2.5f);
int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12;
if (dataGridView1.Columns["Column1"].Visible == false)
{
x1 = 0;
}
else
{
x1 = dataGridView1.Columns[0].Width;
}
if (dataGridView1.Columns["Column2"].Visible == false)
{
x2 = 0;
}
else
{
x2 = 100;
}
if (dataGridView1.Columns["Column3"].Visible == false)
{
x3 = 0;
}
else
{
x3 = 70;
}
if (dataGridView1.Columns["Column4"].Visible == false)
{
x4 = 0;
}
else
{
x4 = 250;
}
if (dataGridView1.Columns["Column5"].Visible == false)
{
x5 = 0;
}
else
{
x5 = 50;
}
if (dataGridView1.Columns["Column6"].Visible == false)
{
x6 = 0;
}
else
{
x6 = 100;
}
if (dataGridView1.Columns["Column7"].Visible == false)
{
x7 = 0;
}
else
{
x7 = 100;
}
if (dataGridView1.Columns["Column8"].Visible == false)
{
x8 = 0;
}
else
{
x8 = 80;
}
if (dataGridView1.Columns["Column9"].Visible == false)
{
x9 = 0;
}
else
{
x9 = 80;
}
if (dataGridView1.Columns["Column10"].Visible == false)
{
x10 = 0;
}
else
{
x10 = 70;
}
if (dataGridView1.Columns["Column11"].Visible == false)
{
x11 = 0;
}
else
{
x11 = 100;
}
if (dataGridView1.Columns["Column12"].Visible == false)
{
x12 = 0;
}
else
{
x12 = 100;
}
if (dataGridView1.Columns["Column1"].Visible == true)
{
#region Pallet
e.Graphics.DrawRectangle(p, new Rectangle(10, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column2"].Visible == true)
{
#region UPC No
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1, 100, 100, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1, 100, 100, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column3"].Visible == true)
{
#region Item No
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2, 100, 70, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString(dataGridView1.Columns[2].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2, 100, 70, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column4"].Visible == true)
{
#region Item Description
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3, 100, 250, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString(dataGridView1.Columns[3].HeaderText.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3, 100, 250, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column5"].Visible == true)
{
#region Cases
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4, 100, 50, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Cases", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4, 100, 50, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column6"].Visible == true)
{
#region LOTNo
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5, 100, 100, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("LOTNo", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5, 100, 100, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column7"].Visible == true)
{
#region PONo
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6, 100, 100, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("PONo", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6, 100, 100, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column8"].Visible == true)
{
#region Expiration Date
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 +x7 , 100, 80, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Exp.Date", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 , 100, 80, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column9"].Visible == true)
{
#region Receieveing Date
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8, 100, 80, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Rec. Date", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8, 100, 80, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column10"].Visible == true)
{
#region Person
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9, 100, 70, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Person", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9, 100, 70, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column11"].Visible == true)
{
#region Overstock Location
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10, 100, 100, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Stock Location", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10, 100, 100, dataGridView1.Rows[0].Height + 5));
#endregion
}
if (dataGridView1.Columns["Column12"].Visible == true)
{
#region Picking Location
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11, 100, 100, dataGridView1.Rows[0].Height + 5));
e.Graphics.DrawString("Picking Location", dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11, 100, 100, dataGridView1.Rows[0].Height + 5));
#endregion
}
int i = 0;
height = 105;
while (i < dataGridView1.Rows.Count)
{
height += dataGridView1.Rows[0].Height;
if (dataGridView1.Columns["Column1"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column2"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1, height, 100, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1, height, 100, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column3"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2, height, 70, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2, height, 70, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column4"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3, height, 250, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3, height, 250, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column5"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4, height, 50, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4, height, 50, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column6"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5, height, 100, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[5].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5, height, 100, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column7"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6, height, 100, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[6].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6, height, 100, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column8"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7, height, 80, dataGridView1.Rows[0].Height ));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[7].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7, height, 80, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column9"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7, height, 80, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[8].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8, height, 80, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column10"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9, height, 70, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[9].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9, height, 70, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column11"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10, height, 100, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[10].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10, height, 100, dataGridView1.Rows[0].Height));
}
if (dataGridView1.Columns["Column12"].Visible == true)
{
e.Graphics.DrawRectangle(p, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11, height, 100, dataGridView1.Rows[0].Height));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[11].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new Rectangle(10 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11, height, 100, dataGridView1.Rows[0].Height));
}
i++;
}
}
请帮帮我。我被困住了。 在此先感谢....