如何从C#中的DataGridView打印数据? [英] How can I print data from a DataGridView in C#?

查看:162
本文介绍了如何从C#中的DataGridView打印数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个程序,我需要打印显示在一个DataGridView控制法案数据。
我想知道的C如何打印在DataGridView的数据$ C $。

I am developing a program, and I need to print the Bills data that is displayed in a DataGridView control.
I want to know the code how to print the data in the DataGridView.

我在使用Visual Studio 2008和C#3.5

I am using Visual Studio 2008 and C# 3.5

推荐答案

没有内置到的DataGridView 任何打印的支持,所以你必须要实现这个你自己。有几个可能的解决方案:

There isn't any printing support built into the DataGridView, so you'll have to implement this yourself. There are a couple of possible solutions:

  1. 的WinForms的确实的提供了一个标准的打印系统,您可以利用打印出你的的DataGridView 控件的内容。您将要使用的PrintDocument 类,因此相关的<一个href="http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx">documentation是一个伟大的地方开始阅读。这种方法的好处是,它可以让你完全控制打印文档的格式和布局。

  1. WinForms does provide a standard printing system, which you can harness to print out the contents of your DataGridView control. You'll want to use the PrintDocument class, so the relevant documentation is a great place to start reading. The advantage of this method is that it allows you complete control over the format and layout of the printed document.

您可以从您的的DataGridView 到Microsoft Excel导出数据,然后从那里打印。 Excel提供了更强大的,内置的打印支持。

You could export the data from your DataGridView to Microsoft Excel, and then print it from there. Excel has much more robust, built-in printing support.

如果你不感兴趣的滚动自己的解决方案,您可以浏览 $ C $的CProject 了解一些已经设计的解决方案。例如:

If you're not interested in rolling your own solution, you can browse CodeProject for some already designed solutions. For example:

  • The DataGridViewPrinter Class
  • Printing a DataGridView on DotNet Framework
  • Another DataGridView Printer
  • DataGridView Printing by Selecting Columns and Rows

即使你没有找到一个适合您的具体需求的插入式解决方案,你也许可以得到如何去使用公布的code,例如,创建此功能,你自己是一个好主意。

Even if you don't find a drop-in solution that fits your exact needs, you can probably get a good idea of how to go about creating this functionality yourself by using the published code as an example.

如果您了一个真正哈克的解决方案(和你没有任何的欲望或需要自定义打印输出的布局和设计),你可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx"><$c$c>DrawToBitmap方法,通过每一个控制曝光。这是一个非常快速和肮脏的方式,吸引,因为它出现在屏幕上以一个位图的DataGridView 控制的精确图像C $ C>,然后你就可以直接传递到您的打印机。

If you're up for a really hacky solution (and you don't have any desire or need to customize the layout or design of the printed output), you could use the DrawToBitmap method exposed by every control. This is a really quick-and-dirty approach that draws an exact image of the DataGridView control as it appears on your screen to a Bitmap, which you can then pass directly to your printer.

这篇关于如何从C#中的DataGridView打印数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆