求救 ,怎么将超宽的列打印到下一张纸,下面是代码,求指教
private static void PrintDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int tmpWidth, i;
int tmpTop = e.MarginBounds.Top;
int tmpLeft = e.MarginBounds.Left;
try
{
// Before starting first page, it saves Width & Height of Headers and CoulmnType
if (PageNo == 1)
{
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible) continue;
// Skip if the current column not selected
if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;
tmpWidth = GridCol.Width;
HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
// Save width & height of headres and ColumnType
ColumnLefts.Add(tmpLeft);
ColumnWidths.Add(tmpWidth);
ColumnTypes.Add(GridCol.GetType());
tmpLeft += tmpWidth;
if (tmpLeft > e.MarginBounds.Width)
tmpLeft = e.MarginBounds.Left;
}
}
// Printing Current Page, Row by Row
while (RowPos <= dgv.Rows.Count - 1)
{
DataGridViewRow GridRow = dgv.Rows[RowPos];
if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
{
RowPos++;
continue;
}
CellHeight = GridRow.Height;
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("宋体", 12, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top -
e.Graphics.MeasureString(PrintTitle, new Font("宋体", 12, FontStyle.Bold),
e.MarginBounds.Width).Height - 13);
String s = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
e.Graphics.DrawString(s, new Font("宋体", 12, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
e.Graphics.MeasureString(s, new Font("宋体", 12, FontStyle.Bold),
e.MarginBounds.Width).Width), e.MarginBounds.Top -
e.Graphics.MeasureString(PrintTitle, new Font(new Font("宋体", 12, FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height - 13);
// Draw Columns
tmpTop = e.MarginBounds.Top;
i = 0;
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible) continue;
if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText))
continue;
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF((int)ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight), StrFormat);
i++;
}
NewPage = false;
tmpTop += HeaderHeight;
}
// Draw Columns Contents
i = 0;
foreach (DataGridViewCell Cel in GridRow.Cells)
{
if (!Cel.OwningColumn.Visible) continue;
if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
continue;
// For the TextBox Column
if (((Type)ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" ||
((Type)ColumnTypes[i]).Name == "DataGridViewLinkColumn")
{
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF((int)ColumnLefts[i], (float)tmpTop,
(int)ColumnWidths[i], (float)CellHeight), StrFormat);
}
// Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i],
tmpTop, (int)ColumnWidths[i], CellHeight));
i++;
}
tmpTop += CellHeight;
}
RowPos++;
// For the first page it calculates Rows per Page
if (PageNo == 1) RowsPerPage++;
}
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);
}
}
private static void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e,
int RowsPerPage)
{
double cnt = 0;
// Detemining rows number to print
if (PrintAllRows)
{
if (dgv.Rows[dgv.Rows.Count - 1].IsNewRow)
cnt = dgv.Rows.Count - 2; // When the DataGridView doesn't allow adding rows
else
cnt = dgv.Rows.Count - 1; // When the DataGridView allows adding rows
}
else
cnt = dgv.SelectedRows.Count;
// Writing the Page Number on the Bottom of Page
string PageNum = " 第 " + PageNo.ToString()
+ " 页,共 " + Math.Ceiling((double)(cnt / RowsPerPage)).ToString()
+ " 页";
e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black,
e.MarginBounds.Left + (e.MarginBounds.Width -
e.Graphics.MeasureString(PageNum, dgv.Font,
e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top +
e.MarginBounds.Height + 31);
}
}
}