在C#上打印datagridview [英] Print datagridview on C#

查看:72
本文介绍了在C#上打印datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像的数据网格视图。现在我要打印那个。什么是简单的方法。我尝试了一些代码。但就这种方式只打印datagridview的当前屏幕大小。不是所有的数据。请帮助我。我正在使用.net 2013和c#

谢谢



我尝试了什么:



  private   void  button1_Click ( object  sender,EventArgs e)
{
Bitmap img1;
位图img2;

img1 = new 位图( @ d:\a1.png);
img2 = new 位图( @ d :\a2.png);

.dataGridView1.Rows.Add( date ID,img1);
this .dataGridView1.Rows.Add( date ID,img2);


foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Height = 100 ;

}
}



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

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

解决方案

它只打印可见部分的原因很简单:这就是你告诉它要做的事情。 br />
当您使用Control.DrawToBitmap时,它就是这样做的:为控件发出一个Paint事件,并将位图的Graphics上下文传递给Control.Paint事件处理程序。所以你在位图中得到的与你在屏幕上得到的完全一样,并且永远不会有所不同。



如果你想打印所有DataGridViewData,你将需要循环遍历行和列(或更好的底层DataSource的行和列)并自己绘制它们,使用Graphics.DrawText,Graphics.DrawImage等手动定位,调整大小并将数据呈现到打印中输出

i have data grid view with the images. now i want to print that one. what is the easy way.i tried some code.but on that way only print current screen size of the datagridview.not all of data. pls helpme. im using .net 2013 and c#
Thank you

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            Bitmap img1;
            Bitmap img2;

            img1 = new Bitmap(@"d:\a1.png");
            img2 = new Bitmap(@"d:\a2.png");

            this.dataGridView1.Rows.Add("date" ,"ID", img1);
            this.dataGridView1.Rows.Add("date", "ID", img2);


            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Height = 100;

            }
        }

      

        private void button2_Click(object sender, EventArgs e)
        {
            printDocument1.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);
        }

解决方案

The reason it only prints the visible section is simple: that is what you told it to do.
When you use Control.DrawToBitmap it does exactly that: issues a Paint event for the control and passes the Graphics context for the bitmap to the Control.Paint event handler. So what you get in the bitmap is exactly the same as you get on the screen, and can never be different.

If you want to print all the DataGridViewData, you will need to loop through the Rows and Columns (or better the Rows and Columns of the underlying DataSource) and draw them yourself, using Graphics.DrawText, Graphics.DrawImage, and suchlike to manually position, size, and render the data into the printed output.


这篇关于在C#上打印datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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