在多个页面中打印DataGridview

时间:2013-10-31 10:36:04

标签: c# winforms printing datagridview

我想在A4尺寸的页面上打印一个datagridview。我成功地只能打印第一页的预览但无法从第二页打印或预览。我正在提交代码。

Private void printDocument1_PrintPage(object sender,system.Drawing.Printing.PrintPageEventArgs e)
{
  int rowcounter=0;
  int x=0;
  int rowheight=0;
  int colwidth=0;
  rowheight=dataGridView1.Rows[0].Height;
  colwidth=dataGridView1.Columns[1].width;

  for(int i=1;i<dataGridView1.Columns.count;i++)
     {          e.Graphics.DrawReactangle(Pens.Black,x,0,colwidth,20);
e.Graphics.FillReactangle(Brushes.LightGray,x,0,colwidth,20);
e.Graphics.Drawstring(dataGridView1.Columns[i].HeaderText.ToString(),dataGridView1.Font,Brushes.Black,new ReactangleF(x,0,colwidth,20));
x=x+colwidth;
     }
   While(rowcounter<=dataGridView1.Rows.Count-1)
   {
      int y=0;
      int j=0;
      int i;
      int xaxis=20;
      rowheight=dataGridview1.Rows[0].Height;
      colwidth=dataGridview1.Columns[1].width;

     for(j=0;j<=dataGridView1.Rows.Count-1;j++)
      {
       for(i=1;i<dataGridView1.Columns.Count;i++)
            {
e.Graphics.DrawReactangle(Pens.Black,y,xaxis,colwidth,dataGridView1.Rows[j].Height);
e.Graphics.Drawstring(dataGridView1.Rows[j].cells[i].Value.ToString(),dataGridView1.Font,Brushes.Black,new ReactangleF(y,xaxis,colwidth,dataGridView1.Rows[j].Height));
y=y+colwidth;
             }
       xaxis=xaxis+dataGridView1.Rows[j].Height;
       y=0;
       } 
      rowcount++;
     }
}

Private Void button6_Click(object sender,EventArgs e)
{
printPreViewDialog1.Document=printDocument1;
printPreViewDialog1.ShowDialog();
}

请帮帮我......

感谢。

0 个答案:

没有答案