没有合适的资源?快使用搜索试试~ 我知道了~
DataGridView打印类的实例
5星 · 超过95%的资源 需积分: 9 18 下载量 118 浏览量
2011-10-25
22:19:40
上传
评论 1
收藏 85KB DOC 举报
温馨提示
试读
20页
DataGridView 打印 类 实例 参考,通过实例讲解DataGridView打印!
资源推荐
资源详情
资源评论
DataGridView 打印类——支持特定页打印(1)
2008-07-03 14:02:59|分类: .Net Framework |标签:软件 |字号大中小订阅
经过两天的努力终于完成了一个将 DataGridView 中的数据打印出来的功能。现记录如下:
一 打印类:
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
/**********************
* 2008.07.2
*********************/
namespace Sacesc_wms
{
/// <summary>
/// 该类是专门出来数据网格打印的类
/// </summary>
public class DataGridViewPrinter
{
public static bool isfirst = true;
private DataGridView TheDataGridView; // 要打印的数据网格
private PrintDocument ThePrintDocument; // 用来打印的对象
private bool IsCenterOnPage; // 是否将内容显示在页面中间
private bool IsWithTitle; // 是否打印标题
private string TheTitleText; // 标题内容
private Font TheTitleFont; // 标题字体
private Color TheTitleColor; // 标题颜色
private bool IsWithPaging; // 是否打印页号
static int CurrentRow; // 要打印的行(在数据网格中)
/// <summary>
/// 一行数据是否要分页打印
/// </summary>
public static bool zoom = false;
/// <summary>
/// 用来统计总页数
/// </summary>
public static int PageNumber; //总页数
/// <summary>
/// 一行数据需要分成多少页才能大完
/// </summary>
public static int fenye = 1;
/// <summary>
/// 每页打印的行数
/// </summary>
public static int pageRows = 0;
/// <summary>
/// 保存总页数
/// </summary>
public int pages = 0;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY; //y 坐标
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
//打印列的开始/结束坐标点列表
private List<int[]> mColumnPoints;
//当网格不能在一页显示时 保存位置
private List<float> mColumnPointsWidth;
private int mColumnPoint;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="aDataGridView">数据网格对象</param>
/// <param name="aPrintDocument">打印对象</param>
/// <param name="CenterOnPage">是否居中 dayin</param>
/// <param name="WithTitle">是否打印标题</param>
/// <param name="aTitleText">标题内容</param>
/// <param name="aTitleFont">标题字体</param>
/// <param name="aTitleColor">标题颜色</param>
/// <param name="WithPaging">是否打印页号</param>
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool
CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
//如果是纵向打印
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
//横向打印
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
//设置页边距
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
// 第一次打印行为首行
CurrentRow = 0;
}
//计算行高 列宽 和整个数据网格的宽度
private void Calculate(Graphics g)
{
if (PageNumber == 0)
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView.Columns.Count; i++)
{
tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
//如果没有指定标题字体,则使用默认字体
if (tmpFont == null)
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString(TheDataGridView.Columns[i].HeaderText, tmpFont);
tmpWidth = tmpSize.Width;
tmpWidth = TheDataGridView.Columns[i].Width;
RowHeaderHeight = tmpSize.Height;
for (int j = 0; j < TheDataGridView.Rows.Count; j++)
{
tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
//如果没有设置正文字体 则使用默认字体
if (tmpFont == null)
tmpFont = TheDataGridView.DefaultCellStyle.Font;
tmpSize = g.MeasureString("Anything", tmpFont);
RowsHeight.Add(tmpSize.Height);
tmpSize =
g.MeasureString(TheDataGridView.Rows[j].Cells[i].EditedFormattedValue.ToString(), tmpFont);
if (tmpSize.Width > tmpWidth)
tmpWidth = tmpSize.Width;
}
if (TheDataGridView.Columns[i].Visible)
TheDataGridViewWidth += tmpWidth;
ColumnsWidth.Add(tmpWidth);
}
int k = 0;
//通过页宽和数据网格宽度来定义开始/结束列
//使用这个值来决定 哪些列在哪页
//同时决定每页显示的最大列数
int mStartPoint = 0;
for (k = 0; k < TheDataGridView.Columns.Count; k++)
if (TheDataGridView.Columns[k].Visible)
{
mStartPoint = k;
break;
}
int mEndPoint = TheDataGridView.Columns.Count;
for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
剩余19页未读,继续阅读
资源评论
- xiaomo03122012-05-02恩,不错的代码,刚刚谢谢你的代码
- lanlong882012-08-14可以实现打印的功能
mluser
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功