c# datagridview 打印只打一页,不能多页打印,请高手帮忙。

yuxuefa 2013-12-17 06:58:30
首先是一个类:


namespace schoolInformation
{

public class Printer
{
private DataGridView dataview;
private PrintDocument printDoc;
//打印有效区域的宽度
int width;
int height;
int columns;
double Rate;
bool hasMorePage = false;
int currRow = 0;
int rowHeight = 20;
//打印页数
int PageNumber;
//当前打印页的行数
int pageSize = 20;
//当前打印的页码
int PageIndex;

private int PageWidth; //打印纸的宽度
private int PageHeight; //打印纸的高度
private int LeftMargin; //有效打印区距离打印纸的左边大小
private int TopMargin;//有效打印区距离打印纸的上面大小
private int RightMargin;//有效打印区距离打印纸的右边大小
private int BottomMargin;//有效打印区距离打印纸的下边大小

int rows;
public Printer(DataGridView dataview, PrintDocument printDoc)
{

this.dataview = dataview;
this.printDoc = printDoc;
PageIndex = 0;
//获取打印数据的具体行数
this.rows = dataview.RowCount;

this.columns = dataview.ColumnCount;
//判断打印设置是否是横向打印
if (!printDoc.DefaultPageSettings.Landscape)
{

PageWidth = printDoc.DefaultPageSettings.PaperSize.Width;
PageHeight = printDoc.DefaultPageSettings.PaperSize.Height;

}
else
{

PageHeight = printDoc.DefaultPageSettings.PaperSize.Width;
PageWidth = printDoc.DefaultPageSettings.PaperSize.Height;

}
LeftMargin = printDoc.DefaultPageSettings.Margins.Left;
TopMargin = printDoc.DefaultPageSettings.Margins.Top;//上面有装订线
RightMargin = printDoc.DefaultPageSettings.Margins.Right;
BottomMargin = printDoc.DefaultPageSettings.Margins.Bottom;


height = PageHeight - TopMargin - BottomMargin - 2;
width = PageWidth - LeftMargin - RightMargin - 2;

double tempheight = height;
double temprowHeight = rowHeight;
while (true)
{
string temp = Convert.ToString(tempheight / Math.Round(temprowHeight, 3));
int i = temp.IndexOf('.');
double tt = 100;
if (i != -1)
{
tt = Math.Round(Convert.ToDouble(temp.Substring(temp.IndexOf('.'))), 3);
}
if (tt <= 0.01)
{
rowHeight = Convert.ToInt32(temprowHeight);
break;
}
else
{
temprowHeight = temprowHeight + 0.01;

}
}
pageSize = height / rowHeight;
if ((rows + 1) <= pageSize)
{
pageSize = rows + 1;
PageNumber = 1;
}
else
{
PageNumber = rows / (pageSize - 1);
if (rows % (pageSize - 1) != 0)
{
PageNumber = PageNumber + 1;
}

}


}


public bool InitPrint()
{

PageIndex = PageIndex + 1;
MessageBox.Show(PageIndex.ToString());
if (PageIndex == PageNumber)
{
hasMorePage = false;
if (PageIndex != 1)
{
pageSize = rows % (pageSize - 1) + 1;
}
}
else
{
hasMorePage = true;
}

return hasMorePage;

}
//打印头
private void DrawHeader(Graphics g)
{

Font font = new Font("宋体", 11, FontStyle.Bold);
int temptop = (rowHeight / 2) + TopMargin + 1;
int templeft = LeftMargin + 1;

for (int i = 0; i < this.columns; i++)
{
string headString = this.dataview.Columns[i].HeaderText;

float fontHeight = g.MeasureString(headString, font).Height;
float fontwidth = g.MeasureString(headString, font).Width;
float temp = temptop - (fontHeight) / 3;
g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));
templeft = templeft + (int)(this.dataview.Columns[i].Width / Rate) + 1;
}

}
//画表格
private void DrawTable(Graphics g)
{

Rectangle border = new Rectangle(LeftMargin, TopMargin, width, (pageSize) * rowHeight);
g.DrawRectangle(new Pen(Brushes.Black, 2), border);
for (int i = 1; i < pageSize; i++)
{
if (i != 1)
{
g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
}
else
{
g.DrawLine(new Pen(Brushes.Black, 2), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
}
}

//计算出列的总宽度和打印纸比率
Rate = Convert.ToDouble(GetDateViewWidth()) / Convert.ToDouble(width);
int tempLeft = LeftMargin + 1;
int endY = (pageSize) * rowHeight + TopMargin;
for (int i = 1; i < columns; i++)
{
tempLeft = tempLeft + 1 + (int)(this.dataview.Columns[i - 1].Width / Rate);
g.DrawLine(new Pen(Brushes.Black, 1), new Point(tempLeft, TopMargin), new Point(tempLeft, endY));
}

}
/**/
/// <summary>
/// 获取打印的列的总宽度
/// </summary>
/// <returns></returns>
private int GetDateViewWidth()
{
int total = 0;
for (int i = 0; i < this.columns; i++)
{
total = total + this.dataview.Columns[i].Width;
}
return total;
}

//打印行数据
private void DrawRows(Graphics g)
{

Font font = new Font("宋体", 11, FontStyle.Regular);
int temptop = (rowHeight / 2) + TopMargin + 1 + rowHeight;

try
{
for (int i = currRow; i < pageSize + currRow - 1; i++)
{
int templeft = LeftMargin + 1;
for (int j = 0; j < columns; j++)
{

string headString = this.dataview.Rows[i].Cells[j].Value.ToString();

float fontHeight = g.MeasureString(headString, font).Height;
float fontwidth = g.MeasureString(headString, font).Width;

float temp = temptop - (fontHeight) / 3;
while (true)
{
if (fontwidth <= (int)(this.dataview.Columns[j].Width / Rate))
{
break;
}
else
{
headString = headString.Substring(0, headString.Length - 1);
fontwidth = g.MeasureString(headString, font).Width;
}
}
g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));

templeft = templeft + (int)(this.dataview.Columns[j].Width / Rate) + 1;
}

temptop = temptop + rowHeight;


}
currRow = pageSize + currRow - 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"数据错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

}

/**/
/// <summary>
/// 在PrintDocument中的PrintPage方法中调用
/// </summary>
/// <param name="g">传入PrintPage中PrintPageEventArgs中的Graphics</param>
/// <returns>是否还有打印页 有返回true,无则返回false</returns>
public bool Print(Graphics g,string title)
{
InitPrint();
DrawTable(g);
DrawHeader(g);
DrawRows(g);



//打印页码
string pagestr = PageIndex + " / " + PageNumber;
Font font = new Font("宋体", 10, FontStyle.Regular);//页脚字体
Font font2 = new Font("黑体", 16, FontStyle.Regular);//标题字体
g.DrawString(pagestr, font, Brushes.Black, new PointF((PageWidth / 2) - g.MeasureString(pagestr, font).Width, PageHeight - (BottomMargin / 2) - g.MeasureString(pagestr, font).Height));
//打印查询的功能项名称
// string temp = dataview.Tag.ToString() + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
// string temp = "班级学生成绩";
string temp = title;
g.DrawString(temp, font2, Brushes.Black, new PointF(PageWidth / 2 + 15 - g.MeasureString(temp, font).Width, 70));//这是标题
g.DrawString(temp, font, Brushes.Black, new PointF(PageWidth - 5 - g.MeasureString(temp, font).Width, PageHeight - 5 - g.MeasureString(temp, font).Height));//这是在页脚
return hasMorePage;





}



}
}



其次是 _PrintPage:

private void printDocument_score_PrintPage(object sender, PrintPageEventArgs e)
{
//设置 打印内容
Graphics g = e.Graphics;
string className = this.className.Text;
string terme = this.label_termeName.Text;
string course = this.courseName.Text;
string title = className + terme + course + "期末成绩";
Printer p = new Printer(dataGridView_score, printDocument_score);
p.Print(g, title);
}


结果:只能打印每一页,不能打印第二页,请高手帮忙,我用的是c#
...全文
428 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京大白兔 2015-01-22
  • 打赏
  • 举报
回复
版主,我也碰到这个问题了,你是怎么解决的呢?
feiyun0112 2013-12-23
  • 打赏
  • 举报
回复
Printer p = new Printer(dataGridView_score, printDocument_score); 放到printDocument_score_PrintPage外面初始化
  • 打赏
  • 举报
回复
首先把代码整理一下,当然现嗑《代码之美》有点晚了,把一个代码段整理成一个方法,每个类的行数理论上不要超过500行,不然不利于阅读,我没仔细看你的代码,如果想把所有的数据打印出来,但你却只打出了一页的数据,那么只有一种可能,那就是你只给了一行的代码,好好检查你的数据源才是正途。
yuxuefa 2013-12-19
  • 打赏
  • 举报
回复
如何把hasMorepages专给原_printPage
熙风 2013-12-18
  • 打赏
  • 举报
回复
你搞这么多还不如用水晶报表打印,方便
Andy__Huang 2013-12-18
  • 打赏
  • 举报
回复
你只加载一页的数据啊,打印时你应该不要再设置PageSize,让它返回所有数据
jiaoshiyao 2013-12-18
  • 打赏
  • 举报
回复
现在看代码头疼
feiyun0112 2013-12-18
  • 打赏
  • 举报
回复
private void printDocument_score_PrintPage(object sender, PrintPageEventArgs e)
{
。。。
e.HasMorePages = p.Print(g, title);
}


*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
yuxuefa 2013-12-18
  • 打赏
  • 举报
回复
我还是不明白,如何才能给更多数据,我也用了 hasmorepages啊。能说祥细些吗,最好给一段代码。
yuxuefa 2013-12-18
  • 打赏
  • 举报
回复
引用 6 楼 hdhai9451 的回复:
你只加载一页的数据啊,打印时你应该不要再设置PageSize,让它返回所有数据
这估计也是这个问题,这个代码是我引用别人的,根据我的程序改的,理解的也不是很好,能给我一个代码吗?
yuxuefa 2013-12-18
  • 打赏
  • 举报
回复
[quote=引用 4 楼 feiyun0112 的回复:] private void printDocument_score_PrintPage(object sender, PrintPageEventArgs e) { 。。。 e.HasMorePages = p.Print(g, title); } 这样会出一种现象,那就是重复打一页,好象没有停下来的时候。
  • 打赏
  • 举报
回复
因为你只给了一页的数据源啊
蝶恋花雨 2013-12-17
  • 打赏
  • 举报
回复
推荐 jatoolsPrinter Lodop 详细你搜下吧, 里面可以打印多页。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧