How to print values from a DataGridView control

后端 未结 3 1390
盖世英雄少女心
盖世英雄少女心 2020-12-01 22:32

I have an application that has a DataGridView control, which holds data that I want to print out when the \"print\" button is pushed.

So far I have got

3条回答
  •  悲哀的现实
    2020-12-01 23:15

    1. Open Visual Studio 2010.
    2. Create new project and give appropriate name.
    3. Drag and drop datagridview in your form from toolbox.
    4. Add data in the datagridview.
    5. Drag and drop the printDocument from toolbox.
    6. Add a button for print on the form.
    7. Double click to get the click event of button.
    8. Write the below code in print button click event. printDocument1.Print();
    9. Double click on the printDocument to get the Print Page event.
    10. Write the below code for print the datagridview:-

      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);
      }
      

    source https://www.mindstick.com/Articles/1356/datagridview-printing-in-c-sharp

    this will work exactly

提交回复
热议问题