【问题标题】:printing from datagridview it gives me error message从datagridview打印它给了我错误信息
【发布时间】:2020-09-09 18:08:07
【问题描述】:

enter image description here请有人帮我解决这个问题。有人给了我这段代码,当我尝试时它对我不起作用。问题是当我用 rowIndex 更改 Y 时,打印的值将彼此重叠,如果我用 Y 交换 rowIndex,则会出现错误消息。我不明白我没有包括什么,因为我是这个 vb 的新手。enter image description here

'第一个选项

Dim y0 = 10

Dim rowHeight = 50
For rowIndex = 0 To rowCount - 1
    Dim y = y0 + rowIndex * rowHeight

    e.Graphics.DrawString(DataGridView6.Rows(y).Cells(1).Value, ReportBodyFont, Brushes.Black, 50, 320)
    e.Graphics.DrawString(DataGridView6.Rows(y).Cells(3).Value, ReportBodyFont, Brushes.Blue, 400, 320)

Next

'第二个选项

Dim rowCount As Integer = DataGridView6.Rows.Count
For i = 0 To rowCount - 1

    e.Graphics.DrawString(DataGridView6.Rows(i).Cells(1).Value, ReportBodyFont, Brushes.Black, 50, 320)
    e.Graphics.DrawString(DataGridView6.Rows(i).Cells(3).Value, ReportBodyFont, Brushes.Blue, 400, 320)

Next

我也尝试了第二个,但它相同,来自 datagridview 的打印值彼此重叠。请有人帮我解决这个问题,我只想打印值而不是表格。请参阅随附的屏幕截图供您参考。

'第三个选项

Dim rowCount As Integer = DataGridView6.Rows.Count

For i = 0 To rowCount - 1
Dim y0 = 330
    Dim rowHeight = 40

    For rowIndex = 0 To rowCount - 1
        Dim y = y0 + rowIndex * rowHeight

        'Print at y.
        e.Graphics.DrawString(DataGridView6.Rows(i).Cells(1).Value, ReportBodyFont, Brushes.Black, 50, y)
        e.Graphics.DrawString(DataGridView6.Rows(i).Cells(3).Value, ReportBodyFont, Brushes.Blue, 400, y)
             
    Next

Next

【问题讨论】:

  • 您的拉绳总是打印在同一个位置。
  • Dim y = y0 + rowIndex * rowHeight y 不是您的行号,这就是您收到错误的原因。
  • 那么行是什么?
  • 您在第二个选项中选择正确,因为“i”代表行号。但一切都在同一个地方打印:50, 320。这就是第一个选项中的“y”变量的来源。
  • 是的,“i”是行,我理解你的观点,但这不是解决方案,即使我改变垂直和水平对齐方式相同,它在 datagridview 的第二行保持相同的点意思是它们在彼此之上是相同的。这是我在这个问题上的第二天。

标签: vb.net


【解决方案1】:

谢谢 - LarsTech。 这是正确的打印代码。

 Dim rowCount As Integer = DataGridView6.Rows.Count
    Dim y0 = 370
    Dim rowHeight = 20
    For rowIndex = 0 To rowCount - 1
        Dim y = y0 + rowIndex * rowHeight
        e.Graphics.DrawString(DataGridView6.Rows(rowIndex).Cells(1).Value, ReportBodyFont, Brushes.Black, 70, y)
        e.Graphics.DrawString(DataGridView6.Rows(rowIndex).Cells(3).Value, ReportBodyFont, Brushes.Blue, 405, y)
    Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多