求教,关于小票打印机 打印 datagridview后终止的问题

xchjian012 2012-03-27 12:52:52
求教各位大侠,小弟用vs2008自带的打印控件 在小票打印机上 打印winform程序中的部分内容(包含固定内容和datagridview),当使用for循环打完datagridview后,打印机停止打印,后续的固定内容 需 按住小票打印机的出纸按钮,才能打出来,尝试过增加打印末尾的长度,但只有增加300的时候都给打印了出来,但空白纸也拉出很长,增加25 50 100都是终止于for循环打完datagridview。
代码如下,跪求指导....
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
DataTable dt = (DataTable)dataGridView1.DataSource;
int x = 10, y = 0, kuan = 50, gao = 25;
int count = Convert.ToInt32(dataGridView1.Rows.Count.ToString());
Font ff = new Font("宋体", 10);
Font f2 = new Font("宋体", 12);
x += 40;
y += gao;
e.Graphics.DrawString("xx超市发货单", f2, Brushes.Blue, x, y);
y += 40;
x -= 40;
e.Graphics.DrawString("收货单位:", ff, Brushes.Blue, x, y);
x += 60;
e.Graphics.DrawString((string)comboBox1.Text.ToString(), ff, Brushes.Blue, x, y);
y += gao;
x -= 60;
e.Graphics.DrawString("发货时间:", ff, Brushes.Blue, x, y);
x += 60;
e.Graphics.DrawString((string)DateTime.Now.ToLongDateString(), ff, Brushes.Blue, x, y);
y += gao;
x -= 60;
e.Graphics.DrawString("---------------------------------", ff, Brushes.Blue, x, y);
y += gao;
e.Graphics.DrawString("商品名称", ff, Brushes.Blue, x, y);
x += 32;
x += 80;
e.Graphics.DrawString("数量", ff, Brushes.Blue, x, y);
x += 32;
e.Graphics.DrawString("单位", ff, Brushes.Blue, x, y);
x += 32;
e.Graphics.DrawString("单价", ff, Brushes.Blue, x, y);
x += 32;
e.Graphics.DrawString("小计", ff, Brushes.Blue, x, y);
x += 35;


foreach (DataColumn column in dt.Columns)
{
switch (column.ColumnName)
{
case "商品名称DataGridViewTextBoxColumn":
case "单价DataGridViewTextBoxColumn":

break;
default:
continue;
}
e.Graphics.DrawString(column.ColumnName, ff, Brushes.Black, x, y);
x += kuan;

}
y += gao;
x = 10;
for (int i = 0; i < count; i++)
{
dataGridView1.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true)
{
for (int j = 1; j <= 5; j++)
{

e.Graphics.DrawString((string)dt.Rows[i].ItemArray[j].ToString(), ff, Brushes.Black, x, y);
if (j == 1)
{
x += 80;
}
x += 32;
}
}
x = 10;
y += gao;
}
e.Graphics.DrawString("---------------------------------", ff, Brushes.Blue, x, y);
y += gao;
e.Graphics.DrawString("合计", ff, Brushes.Blue, x, y);
x += 205;
e.Graphics.DrawString((string)label2.Text.ToString(), ff, Brushes.Blue, x, y);
y += 100; //以下两行是为了增加高度所设置的
e.Graphics.DrawString(" ", ff, Brushes.Blue, x, y);
y += 25;
}
...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
应该是定位问题,定位记得减去自己的本身高度或者宽度
健者天行 2012-03-27
  • 打赏
  • 举报
回复
我之前写过 小票的打印,
不用e.Graphics.DrawString(这样的的,
直接是写文本文件的方式,只不是写的对象是打印机的端口,

希望对你的帮助:


#region Print_CashResult
public static void Print_CashResult(DataTable dt)
{
SafeFileHandle sfh = null;
string instruction = null;
IntPtr ptr = CreateFile(PrintPort, FileAccess.Write, FileShare.Write, 0, FileMode.Open, 0, IntPtr.Zero);
if (ptr.ToInt32() == -1)
{
MessageBox.Show(string.Format("连接打印机:{0} 失败,请检查连接端口是否正确。", PrintPort ));
return;
}

sfh = new SafeFileHandle(ptr, true);
StreamWriter sw = new StreamWriter( new FileStream( sfh, FileAccess.Write), System.Text.Encoding.Default);

StringBuilder sb = new StringBuilder();
DataRow dr = dt.Rows[0]; //一次充值只有1行

sb.AppendLine(string.Format("单据编号:{0}",dr["BillNo"]));
sb.AppendLine(string.Format("{0,-15}{1,23}",ClubName + "(客户联)","充值"));
sb.AppendLine("================================================");
sb.AppendLine(string.Format("{0,-19}{1,18}", "客户编号:" + CustNo, "客户名称:" + CustName));
sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("{0}{1}{2}{3}{4}", "单据编号 ", "充值时间 ", "账户余额 ", "充值金额 ", " 账户金额"));

sb.AppendLine(string.Format("{0,-10}{1,-9:yy-MM-dd}{2,-9}{3,-9}{4,10}", dr["BillNo"], dr["ResultDate"], dr["RemainAccount"].ToString().Replace(".00", ""), dr["ResultMoney"].ToString().Replace(".00", ""), dr["Account"].ToString().Replace(".00", "")));

sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("打印日期:{0:yyyy-MM-dd HH:mm}------操作员:{1,5}",DateTime.Now,dr["Operator"]));

sw.WriteLine(sb); //第1次 打客户联
instruction = Convert.ToChar(29) + instruction + Convert.ToChar(86) + instruction + Convert.ToChar(66) + instruction + Convert.ToChar(100); //切纸
sw.WriteLine(instruction); //切纸

sw.WriteLine(sb.Replace("(客户联)","(留底联)")); //第2次 打(留底联)
sw.WriteLine(instruction); //切纸

sw.Close();
CloseHandle(ptr);

}
#endregion

110,545

社区成员

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

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

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