C#我的窗体中有个datagridview 用dataset获取到的数据集ds
怎样点击一个button2可以将datagridview里的数据实现打印和打印预览,求大神帮帮忙。...
怎样点击一个button2可以将datagridview里的数据实现打印和打印预览,求大神帮帮忙。
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏40(财富值+成长值)
1个回答
展开全部
给段代码你,看看有没有用:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
//按列顺序进行绘制表体
e.Graphics.DrawString(dataGridView1[j, i].Value.ToString(), new Font("宋体", 12,
FontStyle.Regular), Brushes.Black, xpos, ypos);
xpos = xpos + dataGridView1.Columns[0].Width;
}
xpos = e.MarginBounds.Left;
ypos += dataGridView1.Rows[0].Height;
}
public class PrintHelper
{
public delegate void PrintingPage(Object sender, PrintPageEventArgs e);
public static void PrintReporter(PrintingPage printPage) //打印
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printPage);
PrintDialog dlg = new PrintDialog();
dlg.Document = pd;
if (dlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
public static void PrintViewReporter(PrintingPage printPage) //打印预览
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printPage);
ppd.Document = pd;
ppd.ShowDialog();
}
}
调用方法:
PrintHelper.PrintReporter(MyPrintPage); //打印
PrintHelper.PrintViewReporter(MyPrintPage);//打印预览
写一个事件函数MyPrintPage,使用上面的代码绘制报表的内容,将函数名作为参数传入。
private void MyPrintPage(System.Object sender,System.Drawing.Printing.PrintPageEventArgs e)
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
//按列顺序进行绘制表体
e.Graphics.DrawString(dataGridView1[j, i].Value.ToString(), new Font("宋体", 12,
FontStyle.Regular), Brushes.Black, xpos, ypos);
xpos = xpos + dataGridView1.Columns[0].Width;
}
xpos = e.MarginBounds.Left;
ypos += dataGridView1.Rows[0].Height;
}
public class PrintHelper
{
public delegate void PrintingPage(Object sender, PrintPageEventArgs e);
public static void PrintReporter(PrintingPage printPage) //打印
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printPage);
PrintDialog dlg = new PrintDialog();
dlg.Document = pd;
if (dlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
public static void PrintViewReporter(PrintingPage printPage) //打印预览
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printPage);
ppd.Document = pd;
ppd.ShowDialog();
}
}
调用方法:
PrintHelper.PrintReporter(MyPrintPage); //打印
PrintHelper.PrintViewReporter(MyPrintPage);//打印预览
写一个事件函数MyPrintPage,使用上面的代码绘制报表的内容,将函数名作为参数传入。
private void MyPrintPage(System.Object sender,System.Drawing.Printing.PrintPageEventArgs e)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询