首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印DataGridView

打印DataGridView
EN

Stack Overflow用户
提问于 2013-04-07 05:39:51
回答 1查看 5.1K关注 0票数 0

我已经设置了一个表单,其中我有一个包含预订的DataGridView,目前我可以打印DataGridView,但只能打印显示的内容,因此不会打印任何只能通过向下滚动才能看到的数据。

如何更改代码,以便在单击“打印”按钮时将所有内容(包括通过滚动查看的数据)打印到页面?

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class frmViewBookings : Form
    {
        public frmViewBookings()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Form3 mainpage = new Form3();
            mainpage.Show();
            this.Close();
        }

        private void frmViewBookings_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);    
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            this.dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        } 

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-07 08:29:58

你要做的就是创建一个和你想要打印的控件一样大小的虚拟表单,然后将控件添加到虚拟表单中,显示表单并在虚拟表单上打印控件。

下面是我是如何做到的:

代码语言:javascript
复制
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    //Create bitmap
    Bitmap image = new Bitmap(dataGridView1.Width, dataGridView1.Height);
    //Create form
    Form f = new Form();
    //add datagridview to the form
    f.Controls.Add(dataGridView1);
    //set the size of the form to the size of the datagridview
    f.Size = dataGridView1.Size;
    //draw the datagridview to the bitmap
    dataGridView1.DrawToBitmap(image, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));
    //dispose the form
    f.Dispose();
    //print
    e.Graphics.DrawImage(image, 0, 0);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15856342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档