locked
Print Multiple Datagridviews on one page c# RRS feed

  • Pergunta

  • Hello,

    I have two DataGridViews (datagridview1 & datagridview2) both rosters.

    How can I print them both on the one page. Not on 2 different pages.

    Here is the code I have that prints 1 datagridview on 1 page then the other on another page.

           private void button5_Click(object sender, EventArgs e)
            {
                printDocument1.DefaultPageSettings.Landscape = true;
                printDocument1.Print();
                printDocument2.DefaultPageSettings.Landscape = true;
                printDocument2.Print();
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
                dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
                e.Graphics.DrawImage(bm, 0, 0);
    
            }
    
            private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Bitmap bm2 = new Bitmap(this.dataGridView2.Width, this.dataGridView2.Height);
                dataGridView2.DrawToBitmap(bm2, new Rectangle(0, 0, this.dataGridView2.Width, this.dataGridView2.Height));
                e.Graphics.DrawImage(bm2, 0, 0);
            }
    
        }
    }
    Can someone please edit my code to print them both on the one page. Will be greatly appreciated.
    terça-feira, 29 de outubro de 2013 07:29

Respostas

  • Hello Matt,

    I'm a litle confused about this issue. If you tried to put the two datagridviews on a form, why not try print the whole form?

    Just as this page mentioned:

    http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx

    Regards,



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marcado como Resposta Barry Wang quarta-feira, 6 de novembro de 2013 09:55
    quinta-feira, 31 de outubro de 2013 09:04
  •     private void button1_Click(object sender, EventArgs e)
        {
          printDocument1.DefaultPageSettings.Landscape = 
    true;
          printDocument1.DefaultPageSettings.Margins = 
    new Margins(50, 50, 50, 50);
          printDocument1.OriginAtMargins = 
    true;
          printDocument1.Print();
        }
        
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
          
    int w = System.Math.Max(dataGridView1.Width, dataGridView2.Width);
          
    int h = dataGridView1.Height + dataGridView1.Height;
          
    Bitmap bmp = new Bitmap(w,h);
          
    Rectangle r = new Rectangle(0,0,dataGridView1.Width, dataGridView1.Height);
          dataGridView1.DrawToBitmap(bmp,r);
          r.Y = dataGridView1.Height;
          r.Width = dataGridView2.Width;
          r.Height = dataGridView2.Height;
          dataGridView2.DrawToBitmap(bmp, r);
          e.Graphics.DrawImage(bmp, e.MarginBounds);
        }
      }
    }

    • Marcado como Resposta Barry Wang quarta-feira, 6 de novembro de 2013 09:55
    quinta-feira, 31 de outubro de 2013 18:38

Todas as Respostas

  • Hello Matt,

    I'm a litle confused about this issue. If you tried to put the two datagridviews on a form, why not try print the whole form?

    Just as this page mentioned:

    http://msdn.microsoft.com/en-us/library/aa287529(v=vs.71).aspx

    Regards,



    Barry Wang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marcado como Resposta Barry Wang quarta-feira, 6 de novembro de 2013 09:55
    quinta-feira, 31 de outubro de 2013 09:04
  •     private void button1_Click(object sender, EventArgs e)
        {
          printDocument1.DefaultPageSettings.Landscape = 
    true;
          printDocument1.DefaultPageSettings.Margins = 
    new Margins(50, 50, 50, 50);
          printDocument1.OriginAtMargins = 
    true;
          printDocument1.Print();
        }
        
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
          
    int w = System.Math.Max(dataGridView1.Width, dataGridView2.Width);
          
    int h = dataGridView1.Height + dataGridView1.Height;
          
    Bitmap bmp = new Bitmap(w,h);
          
    Rectangle r = new Rectangle(0,0,dataGridView1.Width, dataGridView1.Height);
          dataGridView1.DrawToBitmap(bmp,r);
          r.Y = dataGridView1.Height;
          r.Width = dataGridView2.Width;
          r.Height = dataGridView2.Height;
          dataGridView2.DrawToBitmap(bmp, r);
          e.Graphics.DrawImage(bmp, e.MarginBounds);
        }
      }
    }

    • Marcado como Resposta Barry Wang quarta-feira, 6 de novembro de 2013 09:55
    quinta-feira, 31 de outubro de 2013 18:38
  • Hi Barry,

    Thanks for that I was unaware I could do that

    Thanks Matt

    quinta-feira, 31 de outubro de 2013 19:48
  • merci beaucoup pour la réponse cher Matt Harber. ce vraiment gentil de ta part. mais est-ce qu'il y a aussi moyen d'imprimer deux ou plusieurs datagridview dans une même page ainsi que des entête a chacune d'eux 
    quinta-feira, 23 de julho de 2020 16:47
  • merci beaucoup pour la réponse cher Matt Harber. ce vraiment gentil de ta part. mais est-ce qu'il y a aussi moyen d'imprimer deux ou plusieurs datagridview dans une même page ainsi que des entête a chacune d'eux 
    Matt has been inactive for six years so doubtful he will reply.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    quinta-feira, 23 de julho de 2020 18:59