帮忙解决这个问题

zhangfengnhac 2010-11-13 03:17:31
private void button1_Click(object sender, EventArgs e)
{

printDialog1.ShowDialog(); //设置打印文档
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.ShowDialog();


}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

//e.Graphics.DrawString(dataGridView1.Columns.ToString(), new Font("宋体", 10, FontStyle.Regular), Brushes.Black, 260, 400);
try
{
DataTable dt = (DataTable)dataGridView1.DataSource;

int r = 0;
int c = 20;
for (int i = 0; i < dt.Rows.Count; i++)
{

for (int j = 0; j < dt.Columns.Count; j++)
{

e.Graphics.DrawString(dt.Rows[i][j].ToString(), new Font("宋体", 10, FontStyle.Regular), Brushes.Black, r, c);
r = r + 60;

}
r = 0;
c += 20;

}
}
catch (Exception ex)
{
}




}

打印没有数据 是空的 程序怎么修改下 高手指点下
...全文
140 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfengnhac 2010-11-13
  • 打赏
  • 举报
回复
太谢谢5楼了 分给你了
没花鹿 2010-11-13
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Attendance
{

/// <summary>
/// <para>打印DataGridView</para>
/// <para>用法:</para>
/// <para> DataGridViewPrint dgvPrint = new DataGridViewPrint(DataGridView dataGridView1);</para>
/// <para> dgvPrint.Print();</para>
/// </summary>

public class DataGridViewPrint
{
private DataGridView dataGridView;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
private int dgvIndex = 0;
private int rowCount = 0;
private int colCount = 0;
private int x = 0;
private int y = 0;
int i = 0;
private int rowGap = 60;
private int leftMargin = 50;
private Font font = new Font("Arial", 10);
private Font headingFont = new Font("Arial", 11, FontStyle.Underline);
private Font captionFont = new Font("Arial", 10, FontStyle.Bold);
private Brush brush = new SolidBrush(Color.Black);
private string cellValue = string.Empty;

public DataGridViewPrint(DataGridView dataGridView)
{
this.dataGridView = dataGridView;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}

private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//for (; dgvIndex < dataGridView.Length; dgvIndex++)

rowCount = dataGridView.Rows.Count-1;
colCount = dataGridView.ColumnCount;
//print headings
y += rowGap;
x = leftMargin;
for (int j = 0; j < colCount; j++)
{
if (dataGridView.Columns[j].Width > 0)
{
cellValue = dataGridView.Columns[j].HeaderText;
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), x, y, dataGridView.Columns[j].Width, rowGap);
e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView.Columns[j].Width, rowGap);
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += dataGridView.Columns[j].Width;
}
}

//print all rows
for (; i < rowCount; i++)
{
y += rowGap;
x = leftMargin;
for (int j = 0; j < colCount; j++)
{
if (dataGridView.Columns[j].Width > 0)
{
cellValue = dataGridView.Rows[i].Cells[j].Value.ToString();
e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView.Columns[j].Width, rowGap);
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGridView.Columns[j].Width;
}
}
if (y >= e.PageBounds.Height - 80)
{
// 允許多頁打印
y = 0;
e.HasMorePages = true;
i++;
return;
}
}
y += rowGap;
for (int j = 0; j < colCount; j++)
{
e.Graphics.DrawString(" ", font, brush, x, y);
}
i = 0;

e.HasMorePages = false;
}

public PrintDocument GetPrintDocument()
{
return printDocument;
}

public void Print()
{
try
{
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 800;
printPreviewDialog.ShowDialog();
}
catch (Exception e)
{
throw new Exception("Printer error." + e.Message);
}
}
}
}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/resigner/archive/2010/05/20/5609745.aspx
没花鹿 2010-11-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhangfengnhac 的回复:]
不行 dataGridView 上的数据不能打印
谁有好的程序源码 将dataGridView 上的数据浏览打印
[/Quote]这个,我还真有,稍等,给你贴过来
zhangfengnhac 2010-11-13
  • 打赏
  • 举报
回复
不行 dataGridView 上的数据不能打印
谁有好的程序源码 将dataGridView 上的数据浏览打印
叶子 2010-11-13
  • 打赏
  • 举报
回复
DataTable dt = dataGridView1.DataSource as DataTable;
zhangfengnhac 2010-11-13
  • 打赏
  • 举报
回复
DataTable dt = (DataTable)dataGridView1.DataSource;

提示有问题 该怎么写代码 好像有类型转化异常
内容概要:本文探讨了将ChatGPT与工业领域结合的应用路径,提出通过将个人工作记录持续输入ChatGPT,将其培养成具备个体经验的工业AI助手。作者结合自身作为设备工程师的实际工作场景,开展实验验证:通过多次录入电机、轴承等设备的振动分析数据与诊断结论,测试ChatGPT在不同数据相似度下的响应表现。实验表明,当输入数据与历史记录高度一致时,ChatGPT能准确复现原有判断;随着工作记录积累越完整,其推理和关联能力越强,未来有望复制个人专业经验。文章强调,工业场景缺乏基础数据,难以直接用新技术解决问题,因此“人即AI,AI即人”的渐进式融合路径更具可行性。 适合人群:从事工业设备管理、状态监测、故障诊断的工程师,以及关注AIGC在垂直行业落地的技术研究人员。 使用场景及目标:①构建基于个人经验的工业AI助手,辅助设备故障诊断决策;②探索大模型在数据稀缺工业场景下的可持续训练路径;③实现经验知识的数字化沉淀与智能复用。 阅读建议:此资源侧重于理念创新与实验验证,读者可借鉴其方法论,结合自身工作流持续输入专业数据,逐步训练专属工业AI助手,并在实践中评估其准确性与演化能力。
内容概要:本文围绕超导磁能储存系统的建模与仿真展开研究,重点基于Simulink平台构建系统动态模型,涵盖超导线圈的电磁特性、能量转换接口、冷却系统及其与电网的交互机制等关键环节。通过设置不同工况进行仿真分析,深入探究系统在电力网络中的动态响应行为、稳定性表现及能量存储效率,评估其在提升电网动态响应能力和运行稳定性方面的应用潜力。研究还结合先进控制策略(如模糊PID、遗传算法优化等),优化系统控制性能,进一步提升储能系统的调节精度与响应速度,为超导储能技术的实际工程应用提供理论依据与仿真验证支持。; 适合人群:具备电力系统、电气工程、自动化或相关专业背景,熟悉Simulink仿真环境与基本电力电子拓扑结构,从事科研、教学或工程开发的高年级本科生、研究生及科研技术人员。; 使用场景及目标:①用于高校科研教学中深入理解超导储能系统的工作原理与动态建模方法;②为电力系统仿真项目提供高保真度的储能单元建模参考;③支撑新型大容量储能系统的设计、控制策略开发与并网性能评估,目标是掌握超导磁能储存系统的全流程建模、仿真分析与控制优化方法。; 阅读建议:建议读者结合Simulink软件动手搭建系统模型,分模块实现超导线圈、变流器、冷却与控制单元,并逐步集成调试;重点关注系统在故障或负荷突变下的动态响应特性,尝试引入不同优化算法进行控制器参数整定,以深化对系统控制机理与性能边界的理解。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧