+ -
当前位置:首页 → 问答吧 → 分页的疑问,为什么照书敲的都不对?

分页的疑问,为什么照书敲的都不对?

时间:2011-12-11

来源:互联网

疑问一,使用了分页后,打印位置是否需要重新定义?
疑问二,为什么照着 C#入门经典 敲得代码都不对?预览的时候就卡住不动了。

每页打印50格,现在我一次打印了60个格子,应打印在2页上,但是分页不成功,打成了一页5行半,分页之前的代码都没问题。一分页就出问题。


C# code

   private void OnPrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;//初始化边框;
            int x = 50; //商品信息打印的开始横坐标;
            int y = 60; //商品信息打印的开始纵坐标;
            int border_x = 40;  //价签边框打印的开始横坐标;
            int border_y = 40;  //价签边框打印的开始纵坐标;
            int border_w = 110;  //价签边框的宽度;
            int border_h = 150;  //价签边框的高度;
            int sums = 0;//边框数量合变量;
            int font_sunms = 0;//打印内容的数量的合;     
            for (int z = 0; z < data_list.Rows.Count - 1; z++)
            {
                sums = sums + int.Parse(data_list.Rows[z].Cells[6].Value.ToString().Trim());       
                //计算第字段“数量”列的和,计算需要打印的边框数
            }
            //画边框
            for (int i = 0; i < sums; i++)
            {
                //边框位置计算,每行10个格,打印10格后换行
                if (i % 10 == 0)
                {
                    if (i == 0)
                    {
                        //首格位置
                        border_x = 40;
                        border_y = 40;
                    }
                    else
                    { 
                        //10格换行
                        border_x = 40;
                        border_y = border_y + border_h;
                    }
                }
                else
                { 
                    //打印一个格后,右移动坐标 
                    border_x = border_x + border_w;
                }
                g.DrawRectangle(new Pen(Color.Black, 1), border_x, border_y, border_w, border_h);  
            }
            //循环遍历DataGridView中的所有行,取得打印内容和位置
            for (int j = 0; j < data_list.Rows.Count - 1; j++)
            {
                //每条记录的打印数量
                for (int p = 0; p < int.Parse(data_list.Rows[j].Cells[6].Value.ToString()); p++)
                {
                    //DataGridView单行打印位置计算   
                    if (font_sunms % 10 == 0)
                    {
                        if (font_sunms == 0)
                        {
                            //首格位置
                            x = 50;
                            y = 60;
                        }
                        else
                        {
                            //打印10格换行
                            x = 50;
                            y = y + border_h;
                        }
                    }
                    else
                    {
                        //打印一个格后,右移动坐标
                        x = x + border_w;
                    }
                    font_sunms = font_sunms + 1;          
                    foreach (object cell in data_list.Rows[j].Cells)
                    {                  
                        //判断DataGridView是否为空
                        DataGridViewCell d = (DataGridViewCell)cell;
                        if (d.Value == null)
                        {
                            MessageBox.Show("数据输入不完整", "错误提示);
                            return;
                        }
                        //取得DATAGRIDVIEW单行的内容
                        if (data_list.Rows[j].Cells[0].Value.ToString().Length > 7)
                        {
                            //省略...
                        } 
                        else
                        {
                            //省略...                           
                        }
                        //将单行信息放入控件
                        lines = t_text.Text.Split('\r');
                        }                     
                      //这就是书上的方法,一预览就卡住了
                      while(linesprinted<lines.Length) 
                      {
     e.Graphics.DrawString(lines[linesprinted++], new Font("Arial", 7), Brushes.Black, x, y);
                            if(font_sunms > 50)
                            {
                                e.HasMorePages = true;
                                return;
                            }
                            linesprinted = 0;
                            e.HasMorePages = false;
                            return;

                      }                           
                        
                }
                
            }
            lines = null;//回收释放字符串数组       
       }


作者: zln0118   发布时间: 2011-12-11

CSDN都没人知道吗?

作者: zln0118   发布时间: 2011-12-11

有的书上的代码有很多的bug

有个高手给的打印控件.试试 
<%@ Page language= "c# " Codebehind= "dy.aspx.cs " AutoEventWireup= "false " Inherits= "GKCX.dy " %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " > 
<html> 
<head> 
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 "> 
<title> 看看 </title> 
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 "> 
<!--media=print 这个属性可以在打印时有效--> 
<style media= "print "> 
.Noprint{display:none;} 
.PageNext{page-break-after: always;} 
</style> 
<style> 
.tdp 

  border-bottom: 1 solid #000000; 
  border-left: 1 solid #000000; 
  border-right: 0 solid #ffffff; 
  border-top: 0 solid #ffffff; 

.tabp 

  border-color: #000000 #000000 #000000 #000000; 
  border-style: solid; 
  border-top-width: 2px; 
  border-right-width: 2px; 
  border-bottom-width: 1px; 
  border-left-width: 1px; 

.NOPRINT { 
font-family: "宋体 "; 
font-size: 9pt; 


</style> 
</head> 
<body> 
<center class= "Noprint "> 
<p> 
<OBJECT id= "WebBrowser " classid= "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height= "0 " width= "0 "> 
</OBJECT> 
<input type= "button " value= "打印 " onclick= "document.all.WebBrowser.ExecWB(6,1) "> <input type= "button " value= "直接打印 " onclick= "document.all.WebBrowser.ExecWB(6,6) "> 
<input type= "button " value= "页面设置 " onclick= "document.all.WebBrowser.ExecWB(8,1) "> 
</p> 
<p> 
<input type= "button " value= "打印预览 " onclick= "document.all.WebBrowser.ExecWB(7,1) "> 
<br /> 
</p> 
<hr align= "center " width= "90% " size= "1 " noshade> 
</center> 
<table width= "90% " border= "0 " align= "center " cellpadding= "2 " cellspacing= "0 " class= "tabp "> 
<tr> 
<td colspan= "3 " class= "tdp "> 第1页 </td> 
</tr> 
<tr> 
<td width= "29% " class= "tdp "> &nbsp; </td> 
<td width= "28% " class= "tdp "> &nbsp; </td> 
<td width= "43% " class= "tdp "> &nbsp; </td> 
</tr> 
<tr> 
<td colspan= "3 " class= "tdp "> &nbsp; </td> 
</tr> 
<tr> 
<td colspan= "3 " class= "tdp "> <table width= "100% " border= "0 " cellspacing= "0 " cellpadding= "0 "> 
<tr> 
<td width= "50% " class= "tdp "> <p> 这样的报表 </p> 
<p> 对一般的要求就够了。 </p> 
</td> 
<td> &nbsp; </td> 
</tr> 
</table> 
</td> 
</tr> 
</table> 
<hr align= "center " width= "90% " size= "1 " noshade class= "NOPRINT "> 
<!--分页--> 
<div class= "PageNext "> </div> 
<table width= "90% " border= "0 " align= "center " cellpadding= "2 " cellspacing= "0 " class= "tabp "> 
<tr> 
<td class= "tdp "> 第2页 </asp:TextBox> </td> 
</tr> 
<tr> 
<td class= "tdp "> 看到分页了吧 </td> 
</tr> 
<tr> 
<td class= "tdp "> &nbsp; </td> 
</tr> 
<tr> 
<td class= "tdp "> &nbsp; </td> 
</tr> 
<tr> 
<td class= "tdp "> <table width= "100% " border= "0 " cellspacing= "0 " cellpadding= "0 "> 
<tr> 
<td width= "50% " class= "tdp "> <p> 这样的报表 </p> 
<p> 对一般的要求就够了。 </p> 
</td> 
<td> &nbsp; </td> 
</tr> 
</table> 
</td> 
</tr> 
</table> 
</body> 
</html> 

作者: hefeng_aspnet   发布时间: 2011-12-11

相关阅读 更多

热门下载

更多