private void PrintDGV(DataGridView dgv)
        {
            this.dataGridView1 = dgv;
            document = new PrintDocument();
            document.PrintPage += new PrintPageEventHandler(document_PrintPage);
        }        void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            int tmpWidth, i;
            int tmpTop = e.MarginBounds.Top;
            int tmpLeft = e.MarginBounds.Left;
           
            foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
            {            
                tmpWidth = GridCol.Width;
                HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                            GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;                // Save width & height of headres and ColumnType
                ColumnLefts.Add(tmpLeft);
                ColumnWidths.Add(tmpWidth);
                ColumnTypes.Add(GridCol.GetType());
                tmpLeft += tmpWidth;
            }
            Graphics g = e.Graphics;
            brush = new SolidBrush(Color.Black);
            p = new Pen(brush, 1);
            f = new Font("Arial", 12); ;
            g.DrawString("biaoti",f ,brush,tmpLeft - 200 ,tmpTop - 50);            tmpTop = e.MarginBounds.Top;
            i = 0;
            foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
            {                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
                    new Rectangle((int)ColumnLefts[i], tmpTop,
                    (int)ColumnWidths[i], HeaderHeight));                e.Graphics.DrawRectangle(Pens.Black,
                    new Rectangle((int)ColumnLefts[i], tmpTop,
                    (int)ColumnWidths[i], HeaderHeight));                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
                    new SolidBrush(GridCol.InheritedStyle.ForeColor),
                    new RectangleF((int)ColumnLefts[i], tmpTop,
                    (int)ColumnWidths[i], HeaderHeight), StrFormat);
                i++;
            }
                      //这里可能错了
           int  k = 0;
          
            for (i = 0; i< dataGridView1.Rows.Count -1;i ++ )
            {
                double CellHeight = this.dataGridView1.Rows[i].Height;
                k = 0;
                tmpTop += HeaderHeight; 
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    DataGridViewCell Cel = dataGridView1.Rows[i].Cells[j];
                    e.Graphics.DrawRectangle(Pens.Black,
                        new Rectangle((int)ColumnLefts[i], tmpTop,
                        (int)ColumnWidths[i], (int)CellHeight));                    e.Graphics.DrawString(Cel.Value .ToString (), Cel.InheritedStyle.Font,
                                           new SolidBrush(Cel.InheritedStyle.ForeColor),
                                           new RectangleF((int)ColumnLefts[k], (float)tmpTop,
                                           (int)ColumnWidths[k], (float)CellHeight), StrFormat);
                    k++;                                 }
            }        }这是我的代码,但是当我打印预览时,只有第一行的表格是完整的,第二,第三行的表格就是不完整的,这是为什么。是我注释为这你可能错了的地方有问题吗。请哪位帮我看一下,可以的话,帮忙改一下。谢谢。

解决方案 »

  1.   

    参考,实现DatagridView的数据打印:
    Printing Multiple Pages and Tabular Printing
      

  2.   

    http://www.codeproject.com/KB/printing/PrintingOfDataGridView.aspx
      

  3.   

    谢谢大家,我还想问一下, 
    tmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
                                          (double)TotalWidth * (double)TotalWidth *
                                          ((double)e.MarginBounds.Width / (double)TotalWidth))));
    HeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
                                GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
    这两句是什么意思啊,留着干嘛的呢?