打印datagridview数据C# [英] Print datagridview data C#

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

问题描述

我有数据gridview。有7行。现在我想在datagrid视图中打印所有行。我尝试了很多方法。根据我的代码,只有6行 打印,缺少1行。我如何在我的datagridview中打印所有行?



我尝试过:



private void button4_Click(object sender,EventArgs e)

{

printDocument1.Print();

}

private void printDocument1_PrintPage_1(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);

}



我当前的输出是在这里

转储 - Google云端硬盘 [ ^ ]

解决方案

我尝试下面的代码来生成图像,它工作正常

  / /  将DataGridView调整为全高。 
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;

// 创建一个位图并在其上绘制DataGridView。
位图位图= 位图( .dataGridView1.Width, .dataGridView1.Height);
dataGridView1.DrawToBitmap(位图, new 矩形( 0 0 .dataGridView1.Width, .dataGridView1.Height)) ;

// 将DataGridView调整回原始高度。
dataGridView1 .Height =身高;

// 将位图保存到文件夹。
位图。保存( @ D:\ temp \ DataGridView.png);


i have data gridview. there have 7 rows. now i want print all of the row in datagrid view. i tried many ways. according to my code there have only 6 rows print and 1 row missing. how i print all of the rows in my datagridview?

What I have tried:

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

my current output is here
dump - Google Drive[^]

解决方案

I tried below code to generate image, its working fine

//Resize DataGridView to full height.
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;

//Create a Bitmap and draw the DataGridView on it.
Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));

//Resize DataGridView back to original height.
dataGridView1.Height = height;

//Save the Bitmap to folder.
bitmap.Save(@"D:\temp\DataGridView.png");


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

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