Vb.net 如何使用printdocument以水平线打印datagridview数据?

Vb.net 如何使用printdocument以水平线打印datagridview数据?,vb.net,visual-studio-2017,Vb.net,Visual Studio 2017,我在SqlServer数据库中有一个查询,该查询的结果在Datagridview中播放,如果您怀疑,结果可以包含0到100个数据,甚至更多数据 我试图水平打印Datagridview的“Identification”列,但没有成功。我在好几个地方做过研究,但什么也没有 遵循表单代码: 导入System.ComponentModel 导入System.Data.SqlClient 导入系统。绘图。打印 公共级frm_relatorio_Entergas '模块中使用的变量 Dim Relator

我在SqlServer数据库中有一个查询,该查询的结果在Datagridview中播放,如果您怀疑,结果可以包含0到100个数据,甚至更多数据

我试图水平打印Datagridview的“Identification”列,但没有成功。我在好几个地方做过研究,但什么也没有

遵循表单代码:

导入System.ComponentModel
导入System.Data.SqlClient
导入系统。绘图。打印
公共级frm_relatorio_Entergas
'模块中使用的变量
Dim RelatorioTitulo作为字符串的报告标题
Dim paginaatual作为整数打印的页码
Dim LinhaAtual As Integer“正在打印的当前行号
Dim LinhasporPagina为整数“每页的行数
将PosicaoDaLinha调暗为正在打印的行的“单个”位置
正在打印“作为整数的Dim寄存器”记录
私人次级监管者()
RelatorioTitulo=“交付报告”
Dim doc As PrintDocument=新的PrintDocument
AddHandler doc.PrintPage,New Printing.PrintPageEventHandler(AddressOf Me.pdRelatorios\u PrintPage)
AddHandler doc.BeginPrint,New Printing.PrintEventHandler(AddressOf Me.Begin\u Print)
Dim dialogo As PrintDialog=新建PrintDialog
'dialogo.Document=doc
'如果(dialogo.ShowDialog=DialogResult.OK),则
作为PrintPreviewDialog的尺寸预览=新的PrintPreviewDialog()
preview.Document=doc
preview.WindowState=FormWindowState.Maximized
preview.PrintPreviewControl.Zoom=1.0
preview.ShowDialog()
"完"
端接头
Private Sub Begin_Print(ByVal sender作为对象,ByVal e作为Printing.PrintEventArgs)
'赋值​​在打印开始时添加到变量
LinhaAtual=0
paginaatual=1
PosicaoDaLinha=0
registro=0
端接头
私有子pdRelatorios_打印页(ByVal sender作为System.Object,ByVal e作为System.Drawing.Printing.PrintPageEventArgs)
'页边距变量(例如,MarginBounds获取表示页边距内部分的矩形区域。)
将边缘标注为单个=e.MarginBounds.Left
将边缘DireIta变为单个=e.MarginBounds.Right
尺寸边缘高级为单个=e.MarginBounds.Top
单条边缘尺寸=e.MarginBounds.Bottom
'笔类定义用于定义直线和曲线的对象
Dim CanetaDaImpressora As Pen=新笔(颜色:黑色,1)
'所用字体的变量(字体类定义文本的特定格式,包括字体、大小和样式属性)。
暗淡字体作为字体
Dim FonteTitulo作为字体
Dim FonteSubTitulo作为字体
暗淡的FonteRodape作为字体
暗淡的字体
Dim font1作为字体
'定义对所用字体的效果
FonteNegrito=新字体(“Arial”,9,FontStyle.Bold)
FonteTitulo=新字体(“Arial”,13,FontStyle.Bold)
FonteSubTitulo=新字体(“Arial”,9,FontStyle.Bold)
FonteRodape=新字体(“Arial”,8)
Fontnormal=新字体(“Calibri”,9)
font1=新字体(“Segoe UI”,8,FontStyle.Bold)
'打印报告的标题
e、 图形.抽绳(RelatorioTitulo,FonteTitulo,笔刷.黑色,MargemEsquerda+200,30,新的StringFormat)
如果DateTimePicker1.Value=DateTimePicker1.Value,则
e、 Graphics.DrawString(DateTimePicker1.Value、FonteSubTitulo、Brushes.Black、MargemEsquerda+900、30、新StringFormat)
其他的
e、 Graphics.DrawString(DateTimePicker1.Value&“-”&DateTimePicker1.Value、FonteSubTitulo、Brushes.Black、MargeMeskerda+900、30、新StringFormat)
如果结束
'定义每页的行数
LinhasporPagina=CInt(e.MarginBounds.Height/新字体(“Calibri”,18).GetHeight(e.Graphics)-2)

而(LinhaAtual另一种选择是:

  • 只获取所需的列值
  • 使用分隔符(如制表符)连接#1中的值
  • 对#2中的值使用Graphics.DrawString方法()
  • 需要考虑以下几点:

  • 您需要从#3开始测量字符串,以查看它是否超出打印文档的边界
  • 如果是这样,那么您将需要打印多个页面,从您停止的地方继续打印
  • 更新

    根据OP的请求,这里有一个示例。请记住,它没有考虑上面列出的注意事项。这将需要在调试中花费一些精力来考虑这些要点:

    Private子pdRelatorios\u PrintPage(发送方作为对象,e作为PrintPageEventArgs)处理pdRelatorios.PrintPage
    Dim title=“入口气关系”
    Dim titleFont=新字体(Font.FontFamily,Convert.ToSingle(Font.Size*1.5),FontStyle.Bold)
    尺寸标题位置=新点F(转换为单个(e.MarginBounds.X*2-e.PageBounds.Width/2),e.MarginBounds.Y)
    尺寸标题尺寸=e.Graphics.MeasureString(标题,标题字体)
    e、 Graphics.DrawString(标题、标题字体、SystemBrush.ControlText、标题位置)
    Dim cellValues=DataGridView1.Rows.Cast(属于DataGridViewRow)。选择(函数(行)row.Cells(0)?.Value?.ToString()
    Dim joinedCellValues=String.Join(Constants.vbTab,cellValues)
    Dim bodyBounds=新矩形F(e.MarginBounds.X,Convert.ToSingle(titleSize.Height+e.MarginBounds.Y),e.PageBounds.Width-e.MarginBounds.X*2,e.PageBounds.Height-e.MarginBounds.Y*2)
    e、 Graphics.DrawString(joinedCellValues、字体、SystemBrush.ControlText、bodyBounds)
    端接头
    
    我可以收集这些值​​在一个文本框中,然后打印出来-但是当它包含很多项时,它们都被按下。你能帮我举个例子吗?在gridview中,有一列“数字”,有值​​从1到10被列出?我可以通过水平打印来理解,请?@JoseGomes我已经更新了我的帖子,加入了一个例子。难以置信,这正是我需要的,谢谢你的帮助。现在我正在设置矩形以适应mor