您的位置:首页 > 其它

用小票打印机打印

2009-10-21 14:22 363 查看
用小票打印机打印datagridview内容。

引用using System.Drawing.Printing;

在winform中添加printDocument1控件,

添加printDocument1的PrintPage的事件printDocument1_PrintPage

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//从数据源中得到数据表对象
DataTable dt = (DataTable)dataGridView1.DataSource;
//设置初始打印位置及打印位置递增量
int x = 30, y = 0, kuan = 70, gao = 25;
//设置字体对象
Font ff = new Font("宋体", 10);
//打印“学生表”三个字
//e.Graphics.DrawString("学生表", ff, Brushes.Blue, x, y);
//打印的行向下一行
y += gao;
//使用循环打印出数据表的列名
//foreach (DataColumn column in dt.Columns)
//{
// //开始打印列
// e.Graphics.DrawString(column.ColumnName, ff, Brushes.Black, x, y);
// //行宽自动增加
// x += kuan;
//}
//向下一行
y += gao;
//回到行头
x = 30;
//使用循环打印数据表中的内容
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
//打印
e.Graphics.DrawString((string)dt.Rows[i].ItemArray[j].ToString(), ff, Brushes.Black, x, y);
//打印一行后,宽度自动变化
x += kuan;
}
//打印一行后,宽度自动归零
x = 30;
//打印一行后,行自动向下加一行
y += gao;
}
}

调用方法:

printDocument1.Print();

即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: