如何在c#窗口应用程序中打印和打印预览代码DATAGRIDVIEW记录 [英] How to print and print preview code DATAGRIDVIEW record in c# window application

查看:184
本文介绍了如何在c#窗口应用程序中打印和打印预览代码DATAGRIDVIEW记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello尊敬的会员

我有problam

如何在窗口应用程序的c#中使用打印预览选项编写print datagridview的代码



我有打印代码,但它直接打印文件没有预览文件,为什么我无法打印所有datagridview项目



可以任意身体帮助我解决我的问题







  private   void  button1_Click( object  sender,EventArgs e) 
{
try
{
printDocument1.Print();

}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}

}

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


}





预付款



祝你好运:)

解决方案

浏览以下文章



http://social.msdn.microsoft.com/Forums/en -US / winformsdatacontrols / thread / dc9d3acf-ccf8-457f-ba31-ef09fb357aee / [ ^ ]


//创建一个名为zpt的函数



int rw = dataGridView1.Rows.Count; //将rw定义为全局变量形式

public void zpt()

{

PrintDialog pd = new PrintDialog();

PrintDocument pdoc = new PrintDocument();

PrinterSettings ps = new PrinterSettings();

Font font = new Font(Arial,10);

PaperSize psz = new PaperSize(Custom,100,200);

pd.Document = pdoc;

pd.Document.DefaultPageSettings .PaperSize = psz;

pdoc.DefaultPageSettings.PaperSize.Height = 820;

pdoc.DefaultPageSettings.PaperSize.Width = 700;

pdoc .PrintPage + = new PrintPageEventHandler(pdoc_PrintPage);

DialogResult res = pd.ShowDialog();

if(res == DialogResult.OK)

{

PrintPreviewDialog prv = new PrintPreviewDialog();

prv。 Document = pdoc;

res = prv.ShowDialog();

if(res == DialogResult.OK)

{

pdoc.Print();

}

}



}

void pdoc_PrintPage(object sender,PrintPageEventArgs e)

{

Graphics graphics = e.Graphics;

Font font = new Font( Courier New,10);

float fontHeight = font.GetHeight();

int startX = 50;

int startY = 65 ;

int Offset = 40;

graphics.DrawString(Welcome to Bakery Shop,new Font(Courier New,10),new SolidBrush(Color.Black ),startX,startY + Offset);

Offset = Offset + 20;

string underLine =--------------- ---------------------------;

graphics.Dr awString(underLine,new Font(Courier New,10),new SolidBrush(Color.Black),startX,startY + Offset);

Offset = Offset + 20;

int a = dataGridView1.Rows.Count;

for(int i = 0;我<一个; i ++)

{

graphics.DrawString(Convert.ToString(dataGridView1.Rows [i] .Cells [0] .Value),new Font(Courier New, 10),新的SolidBrush(Color.Black),startX,startY + Offset);

graphics.DrawString(\t+ Convert.ToString(dataGridView1.Rows [i] .Cells [1 ] .Value),new Font(Courier New,10),新的SolidBrush(Color.Black),startX,startY + Offset);

Offset = Offset + 20;

}

}







private void button1_Click(object发件人,EventArgs e)

{



dataGridView1.Rows.Add();

dataGridView1.Rows [ rw] .Cells [0] .Value = textBox1.Text;

dataGridView1.Rows [rw] .Cells [1] .Value = textBox2.Text;

rw ++;

}



private void button2_Click(object sender,EventArgs e)

{

//关于打印按钮,在您的窗口中显示代码...

zpt();

}

Hello Respected members
I have problam
How to write code for print datagridview with print preview option in c# in window application

I have code for print but its directly print document not Previewing document that why the i am not able to print all datagridview item

Can any body help me for sort out my problam



private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               printDocument1.Print();
               
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }

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

       }



Thans in Advance

Good luck :)

解决方案

go through following article

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/dc9d3acf-ccf8-457f-ba31-ef09fb357aee/[^]


// make a function named zpt

int rw=dataGridView1.Rows.Count; //define rw as globly variable in form
public void zpt()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
Font font = new Font("Arial", 10);
PaperSize psz = new PaperSize("Custom", 100, 200);
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psz;
pdoc.DefaultPageSettings.PaperSize.Height = 820;
pdoc.DefaultPageSettings.PaperSize.Width = 700;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult res = pd.ShowDialog();
if (res == DialogResult.OK)
{
PrintPreviewDialog prv = new PrintPreviewDialog();
prv.Document = pdoc;
res = prv.ShowDialog();
if (res == DialogResult.OK)
{
pdoc.Print();
}
}

}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 50;
int startY = 65;
int Offset = 40;
graphics.DrawString("Welcome to Bakery Shop", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
string underLine = "------------------------------------------";
graphics.DrawString(underLine, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
int a = dataGridView1.Rows.Count;
for (int i = 0; i < a; i++)
{
graphics.DrawString(Convert.ToString(dataGridView1.Rows[i].Cells[0].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
graphics.DrawString("\t"+Convert.ToString(dataGridView1.Rows[i].Cells[1].Value), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
}
}



private void button1_Click(object sender, EventArgs e)
{

dataGridView1.Rows.Add();
dataGridView1.Rows[rw].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[rw].Cells[1].Value = textBox2.Text;
rw++;
}

private void button2_Click(object sender, EventArgs e)
{
// on print Button which is in your window for code this...
zpt();
}


这篇关于如何在c#窗口应用程序中打印和打印预览代码DATAGRIDVIEW记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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