| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 248 人关注过本帖
标题:多页打印
只看楼主 加入收藏
yueyang00
Rank: 2
等 级:论坛游民
帖 子:2
专家分:10
注 册:2011-8-12
收藏
 问题点数:0 回复次数:0 
多页打印
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                //int ii=this.dataGridView1.Rows.Count;
                //获得数据库连接字段
                string conn = this.connection();
                //连接数据库
                con = new SqlConnection(conn);
                //打开数据库连接
                con.Open();
                //要执行的语句
                string select = "select ProductsName as 产品名称,Amount as 成本总价,RiseRange as 上涨幅度,RealityAmount as 实际总价 from info_products";
                //初始化对象
                sda = new SqlDataAdapter(select, con);
                ds = new DataSet();
                //将查询结果放入DataSet中
                sda.Fill(ds, "info_products");
                this.dataGridView1.DataSource = ds.Tables["info_products"];
                //定义一个DataTable并将DataGridView的数据源一起绑定
                DataTable objDataTable = (DataTable)this.dataGridView1.DataSource;
                //定义其他变量
                int fltLines = 50;
                int countPage = (objDataTable.Rows.Count % fltLines == 0 ? objDataTable.Rows.Count / fltLines : objDataTable.Rows.Count / fltLines + 1);
                int curline = 0;
                int nCount = 0;
                //int yulan = 0;
                //int yushu = 1;
                //定义X和Y的坐标
                int startX = 100;
                int startY = 100;
                //头部标签
                e.Graphics.DrawString("产品名称", new Font("Arial", 9), Brushes.Black, 100, 80);
                e.Graphics.DrawString("成本总价", new Font("Arial", 9), Brushes.Black, 200, 80);
                e.Graphics.DrawString("上涨幅度", new Font("Arial", 9), Brushes.Black, 300, 80);
                e.Graphics.DrawString("实际总价", new Font("Arial", 9), Brushes.Black, 400, 80);
                //判断绑定的数据源是否为空,并执行相应的操作
                if (dataGridView1.DataBindings != null)
                {
                    for (int m = 0; m < countPage; m++)
                    {
                        //Iterating through the rows
                        for (int row = 0; row < objDataTable.Rows.Count; row++)
                        {
                            //Storing the rows to objDataRow object
                            DataRow objDataRow = objDataTable.Rows[row];
                            //Iterating through the Columns
                            for (int i = 0; i < objDataTable.Columns.Count; i++)
                            {
                                //Displaying the Values
                                e.Graphics.DrawString(objDataRow[i].ToString(), new Font("Arial", 11), Brushes.Black, startX, startY);
                                startX += 100;
                            }
                            startX = 100;
                            startY += 20;
                            nCount++;
                            if (nCount == fltLines)
                            {
                                break;
                            }
                        }
                        curline += nCount;
                        nCount = 0;
                        //如果未到数据的末尾,继续打印
                        if (curline < dataGridView1.Rows.Count)
                        {
                            e.HasMorePages = true;
                            return;
                        }
                        else
                        {
                            e.HasMorePages = false;
                        }
                    }  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打印时出错。" + ex.Message.ToString());
            }
        }
加上e.HasMorePages = true;在打印预览时无限制的跳动,如不加,则只能打印一页。望高手指教。
搜索更多相关主题的帖子: 数据库连接 连接数据库 private 
2011-08-13 19:47
快速回复:多页打印
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033970 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved