我试图输出一个检索用户数据并将其放入表中的数据网格。当我尝试这样做时,我相信第一行被列名替换,然后另一个空行被添加到底部。任何人都可以看到与此代码块有任何差异,可能会导致它这样做吗?
数据输入:
输出给定:
这是我的代码:
Dim mRow As Integer = 0
Dim newpage As Boolean = True
With DataGridViewONE
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = 770 'e.MarginBounds.Top
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 1
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If mRow = 0 Then
e.Graphics.DrawString(DataGridViewONE.Columns(cell.ColumnIndex).HeaderText,
.Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridViewONE.Rows(cell.RowIndex).
Cells(cell.ColumnIndex).FormattedValue.ToString(),
.Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
修改
我发现ColumnIndex
取代了第一行。从技术上讲,如果将mRow
设置为-1,则不会出现问题,但这样做会产生错误,即索引不能为负值。
If mRow = 0 Then
e.Graphics.DrawString(DataGridViewONE.Columns(cell.ColumnIndex).HeaderText,
.Font, Brushes.Black, rc, fmt)
用行DrawString
替换它会得到我需要的输出但没有列标题