如何更改特定列的 Datagridview 对齐方式以进行打印?
How to change in Datagridview alignment of a specific Column for printing?
我为我奇怪的代码道歉。我是一个完全的新手,只是在学习东西是如何工作的。
我找到了这段代码并更改了一些内容。
我的问题是:
我以某种方式设法使所有列的对齐方式都正确,但我需要为我的第一个 Colum“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
两次,然后您的第二个案例再匹配相同的值两次。在所有情况下,您都将 StringFormat
的 Alignment
设置为 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
我为我奇怪的代码道歉。我是一个完全的新手,只是在学习东西是如何工作的。
我找到了这段代码并更改了一些内容。
我的问题是:
我以某种方式设法使所有列的对齐方式都正确,但我需要为我的第一个 Colum“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
两次,然后您的第二个案例再匹配相同的值两次。在所有情况下,您都将 StringFormat
的 Alignment
设置为 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