ASP.NET基础教程-DataGrid表格控件-打印时让指定每一页的记录数量,并且每页都打印表格的标头
2007-10-11 14:08:14
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://chenxing.blog.51cto.com/240526/45859 |
public static int _pageSize=15; public static string _header;
/// <summary> /// 传入DataGrid的引用和每页记录数,让DataGrid在打印时分页显示
/// </summary> 本文出自 51CTO.COM技术博客/// <param name=“grid”>要打印的DataGrid</param> /// <param name="pageSize">每页显示记录条数</param> 在表格的ItemDataBound事件(在项创建时激发)和ItemCreated事件(在项被数据绑定后激发)中添加如下代码 private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemIndex > 0 && e.Item.ItemIndex % _pageSize == 0) { LiteralControl l = new LiteralControl(); string header = _header.Remove(_header.Length - 12, 12); //把表头代码结尾的</td>\r\n</tr>截去 //加上处理过的表头 l.Text = "</td></tr><tr><td bordercolor=white cellpadding=0 style=\"page-break-after:always\" colspan=\"" + e.Item.Cells.Count + "\"></td></tr> <td colspan=" + + e.Item.Cells.Count + "\"></td>" + header; //如果最后一列是template column得用这句加,注释下面的.原因不明... //e.Item.Cells[e.Item.Cells.Count-1].Controls.Add(l); e.Item.Cells[e.Item.Cells.Count-1].Text += l.Text ; } } private void DataGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e){ //拿到表头的html代码 if (e.Item.ItemIndex == -1) { System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(tw); e.Item.RenderControl(htw); _header = tw.ToString(); } } |


晨星
博客统计信息
热门文章
最新评论
友情链接

