【发布时间】:2013-04-06 21:39:50
【问题描述】:
我已经设置了一个表单,其中有一个带有预订的 DataGridView,目前我可以打印 DataGridView,但只能打印显示的内容,因此不会打印任何只能通过向下滚动才能看到的数据。
如何更改我的代码,以便当我单击打印按钮时,所有内容(包括通过滚动查看的数据)都打印到页面上?
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();
}
}
}
【问题讨论】:
-
为什么不使用报告功能?
-
嗯好主意,如果我不能这样做,我会尝试报告。我是新手,所以需要阅读报告。有什么建议吗?
-
我也想做同样的事情,但我最终还是使用了 Microsoft Reports。真的不难。您必须为您的 DataGridView 数据源创建一个类(如果您没有它)。然后,在报表中,您使用该类的 DataSet 创建一个 Tablix。最后,在运行时,您将 DataGridView 中的 DataSource 列表作为参数传递给报表,然后就完成了。如果你想这样做,告诉我,我可以给你一个代码示例。
标签: c# winforms visual-studio printing datagridview