如何改变在Datagridview对特定列打印?



我为我奇怪的代码道歉。我是一个完全的新手,只是在学习如何工作。

我找到了这段代码,并对其进行了一些修改。

我的问题是:

我设法为所有列对齐是正确的,但我需要我的第一列&;data1 &;左对齐。我可以通过设置或点击按钮来改变它,但打印时它正好与其他对齐。我什么都试过了,但我的知识可能只有2%,所以我真的很感激你的帮助。

我有一个Button1, DataGridView1, PrintPreviewDialog1, PrintDocument1, PrintDialog1

这是一个表单的图片和打印预览的例子:https://drive.google.com/file/d/1VnUzrM9fgiEcJExrXalo7Uo6XKQwT3Sd/view?usp=sharing

这是我的代码


Private mRow As Integer = 0
Private newpage As Boolean = True
Private m_PagesPrinted As Integer = 1
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DataGridView1
.Rows.Add("Plan 1", "50", "2,99", "28,40", "170,90")
.Rows.Add("Plan 27 a", "80", "14,99", "227,85", "1427,05")
.Rows.Add("Plan 27 b", "808", "47,45", "84,85", "14,05")
.Rows.Add("Plan 27 c", "80", "12,21", "77,10", "27,05")
.Rows.Add("Plan 27 d", "470", "14,50", "15,40", "227,99")
.Rows.Add("Plan 27 e", "2", "99,00", "2,84", "4427,67")
.Rows.Add("Plan 27 f", "4", "10,00", "9,48", "7,74")
.Rows.Add("Plan 27 g", "54", "150,50", "46,64", "127,50")
End With
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
newpage = True
'Dim frm As Form = DirectCast(PrintPreviewDialog1, Form)
PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
PrintPreviewDialog1.PrintPreviewControl.Zoom = 0.8
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
' sets it to show '...' for long text
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
Dim newfmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
newfmt.LineAlignment = StringAlignment.Near
'fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y2 As Int32 = 200
Dim rc As Rectangle
Dim x2 As Int32 = 2
Dim h As Int32 = 200
Dim row2 As DataGridViewRow
' print the header text for a new page
'   use a grey bg just like the control
If newpage Then
row2 = DataGridView1.Rows(mRow)
x2 = e.MarginBounds.Left
For Each cell As DataGridViewCell In row2.Cells
Dim x As Integer = 170
Dim y As Integer = 360
Dim xwidth As Integer = 190
Dim yheight As Integer = 20
Dim cellwidth As Integer = 300
Dim cellheight As Integer = 370
Dim fon As New Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold)
Dim rect As New Rectangle(x, 100, xwidth, yheight)
''##########################################################################
''##########################################################################
Dim rek1 As New Rectangle(40, 350, 750, 20)
e.Graphics.DrawRectangle(Pens.Black, rek1)
Dim rek2 As New Rectangle(40, 370, 750, 100)
e.Graphics.DrawRectangle(Pens.Black, rek2)
''##########################################################################
''##########################################################################
e.Graphics.DrawString("Data1                                                                                                      Data2             Data3            Data4                 Data5", fon, Brushes.Black, 42, 351)
Next
y2 += h
End If
newpage = False

' now print the data for each row
Dim thisNDX As Int32
For thisNDX = mRow To DataGridView1.RowCount - 1
' no need to try to print the new row
If DataGridView1.Rows(thisNDX).IsNewRow Then Exit For
row2 = DataGridView1.Rows(thisNDX)
x2 = e.MarginBounds.Left
h = 0
' reset X for data
x2 = e.MarginBounds.Left
' print the data
For Each cell As DataGridViewCell In row2.Cells
If cell.Visible Then
rc = New Rectangle(x2 - 20, y2, cell.Size.Width, cell.Size.Height)

Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.BottomRight
fmt.Alignment = StringAlignment.Far
rc.Offset(-170, -30)
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.BottomRight
fmt.Alignment = StringAlignment.Far
Case Else
fmt.Alignment = StringAlignment.Far
rc.Offset(-170, -30)
End Select
e.Graphics.DrawString(cell.FormattedValue.ToString(), DataGridView1.Font, Brushes.Black, rc, fmt)
x2 += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y2 += h
' next row to print
mRow = thisNDX + 1
If y2 + h > 500 Then
e.HasMorePages = True
mRow -= 1   'causes last row To rePrint On Next page
m_PagesPrinted += 1
newpage = True
Return
End If
Next

End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument1.Print()
End If
End Sub
End Class

谁能告诉我需要改变什么,以便在打印时,第一列左对齐,其余的在右边…

提前感谢!!

你的Select Case没有意义。您的第一个Case匹配DataGridViewContentAlignment.BottomRight两次,然后您的第二个case匹配相同的值两次以上。在所有情况下,都将StringFormatAlignment设置为StringAlignment.Far。我认为它应该更像这样:

Select Case DataGridView1.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.TopLeft,
DataGridViewContentAlignment.MiddleLeft,
DataGridViewContentAlignment.BottomLeft
fmt.Alignment = StringAlignment.Near
Case DataGridViewContentAlignment.TopCenter,
DataGridViewContentAlignment.MiddleCenter,
DataGridViewContentAlignment.BottomCenter
fmt.Alignment = StringAlignment.Center
Case DataGridViewContentAlignment.TopRight,
DataGridViewContentAlignment.MiddleRight,
DataGridViewContentAlignment.BottomRight
fmt.Alignment = StringAlignment.Far

最新更新