http://www.c-sharpcorner.com/Graphics/DataGridPrinterMG.asp

解决方案 »

  1.   

    也可以查考下:
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    int wid=0;
    int count = 3;
    int p1=linesPrinted;
    float linesPerPage = 0;
    int x=e.MarginBounds.Left;
    int y=e.MarginBounds.Top+20;
    printFont=dataGrid1.HeaderFont;
    linesPerPage = e.MarginBounds.Height /printFont.GetHeight(e.Graphics) -3;
    Pen blackPen = new Pen(Color.Black, 2);
    for(int b=0;b<ds.Tables[0].Columns.Count;b++)
    {
    wid+=dataGrid1.TableStyles[0].GridColumnStyles[b].Width;
    }

    if(slocation=="dzuo")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,x,y-20); }
    if(slocation=="dzhong")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,e.MarginBounds.Width/2,y-20); }
    if(slocation=="dyou")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,e.MarginBounds.Width,y-20); }
    if(slocation=="xzuo")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,x,e.MarginBounds.Bottom); }
    if(slocation=="xyou")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,e.MarginBounds.Width-30,e.MarginBounds.Bottom); }
    if(slocation=="xzhong")
    {
    e.Graphics.DrawString(spage1+ipages.ToString()+spage2,dataGrid1.HeaderFont,Brushes.Black,e.MarginBounds.Width/2,e.MarginBounds.Bottom); }


    for(int a=0;a<ds.Tables[0].Columns.Count;a++)
    {
    if(columnwidth[a]!=0)
    {
    e.Graphics.DrawString(dataGrid1.TableStyles[0].GridColumnStyles[a].HeaderText, dataGrid1.HeaderFont, Brushes.Black, x, y);
    x=x+(dataGrid1.TableStyles[0].GridColumnStyles[a].Width*e.MarginBounds.Width/wid);

    }
    }
    y+=(int)printFont.GetHeight(e.Graphics);
    x=e.MarginBounds.Left;
    e.Graphics.DrawLine(blackPen,x,y,x+e.MarginBounds.Right,y);
    y+=2;

    while(count < linesPerPage && linesPrinted>=0)
    {
    if(linesPrinted<=linesPerPage)
    {

    for(i1=0;i1<=p1-1;i1++)
    {
    for(int j=0;j<ds.Tables[0].Columns.Count;j++)
    {
    if(columnwidth[j]!=0)
    {
    e.Graphics.DrawString(lines[i1+linshi,j],dataGrid1.HeaderFont,Brushes.Black,x,y);
    x=x+(dataGrid1.TableStyles[0].GridColumnStyles[j].Width*e.MarginBounds.Width/wid);
    }
    }
    x=e.MarginBounds.Left;
    count++;
    y+=(int)printFont.GetHeight(e.Graphics);

    }
    e.HasMorePages=false;
    return;
                        
    }
    else
    {
    for(i1=0;i1<linesPerPage;i1++)
    {
    for(int j=0;j<ds.Tables[0].Columns.Count;j++)
    {   
    if(columnwidth[j]!=0)
    {
    e.Graphics.DrawString(lines[i1+linshi,j],dataGrid1.HeaderFont,Brushes.Black,x,y);
    x=x+(dataGrid1.TableStyles[0].GridColumnStyles[j].Width*e.MarginBounds.Width/wid);
    }
    }
    x=e.MarginBounds.Left;
    count++;
    y+=(int)printFont.GetHeight(e.Graphics);
    linesPrinted--;


    }
    if(linesPrinted>0)
    e.HasMorePages=true;
    linshi=linshi+i1;
    ipages++;
    return;
    } }
    } private void bprint_Click(object sender, System.EventArgs e)
    {
    if(printDialog1.ShowDialog()==DialogResult.OK)
    {
    printDocument1.Print();
    }
      

  2.   

    http://community.csdn.net/Expert/topic/3205/3205297.xml?temp=.971966
      

  3.   

    不好意思,我想问的是 如果让this.InvokePaint()方法绘制的控件出现在我指定的位置而不是页面的0,0处???