PDA

View Full Version : سوال: Listview print



tstsst
چهارشنبه 17 اسفند 1390, 15:20 عصر
سلام
من یک لیست ویو با 7 ستون چجوری می شه به صورت جدول پرینت گرفت ؟

________________________
یک کد مال یک لیست ویو با 3 ستون (که درست کار می کنه برای 3 ستون) رو تغییر دادم ولی درست عمل نمی کنه البته جدول رو درست رسم می کنه !


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
tablefont = New Font("arial", 10)
headerfont = New Font("arial", 12, FontStyle.Bold)
titlefont = New Font("arial", 18, FontStyle.Bold)
X1 = PrintDocument1.DefaultPageSettings.Margins.Right
With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
pageHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
End With
X2 = X1 + 120
X3 = X2 + 120
X4 = X3 + 120
X5 = X4 + 120
X6 = X5 + 120
X7 = X6 + 120
W1 = X2 - X1
W2 = X3 - X2
W3 = X4 - X3
W4 = X5 - X4
W5 = X6 - X5
W6 = X7 - X6
W6 = pageWidth - W1 - W2 - W3 - W4 - W5 ' X6
If PrintDocument1.DefaultPageSettings.Landscape Then
With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Height - .Margins.Left - .Margins.Right
pageHeight = .PaperSize.Width - .Margins.Top - .Margins.Bottom
End With
X2 = X1 + 60
X3 = X2 + 60
X4 = X3 + 60
X5 = X4 + 60
X6 = X5 + 60
X7 = X6 + 60
X7 = CInt(X6 + pageWidth * 0.6)
W1 = X2 - X1
W2 = X3 - X2
W3 = X4 - X3
W4 = X5 - X4
W5 = X6 - X5
W6 = X7 - X6
W6 = pageWidth - W1 - W2 - W3 - W4 - W5
End If
lineheight = tablefont.GetHeight()
itm = 0
pageno = 0
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Sub drawVerticalLines(ByVal G As Graphics, ByVal y1 As Integer, ByVal y2 As Integer)
G.DrawLine(Pens.Silver, X1, y1, X1, y2)
G.DrawLine(Pens.Silver, X2, y1, X2, y2)
G.DrawLine(Pens.Silver, X3, y1, X3, y2)
G.DrawLine(Pens.Silver, X4, y1, X4, y2)
G.DrawLine(Pens.Silver, X5, y1, X5, y2)
G.DrawLine(Pens.Silver, X6, y1, X6, y2)
G.DrawLine(Pens.Silver, X7, y1, X7, y2)
G.DrawLine(Pens.Silver, X7 + W6, y1, X7 + W6, y2)
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
On Error Resume Next
pageno = pageno + 1
printReportTitle(e.Graphics)
printpagenumber(e.Graphics)
Y = PrintDocument1.DefaultPageSettings.Margins.Top
e.Graphics.DrawString("زمان ", headerfont, Brushes.Black, X1, Y)
e.Graphics.DrawString("شنبه ", headerfont, Brushes.Black, X2, Y)
e.Graphics.DrawString("یکشنبه ", headerfont, Brushes.Black, X3, Y)
e.Graphics.DrawString("دوشنبه ", headerfont, Brushes.Black, X4, Y)
e.Graphics.DrawString("سه شنبه ", headerfont, Brushes.Black, X5, Y)
e.Graphics.DrawString("چهارشنبه ", headerfont, Brushes.Black, X6, Y)
e.Graphics.DrawString("پنجشنبه ", headerfont, Brushes.Black, X7, Y)
Y = Y + 30
Dim YLine As Integer = Y
e.Graphics.DrawLine(Pens.Silver, PrintDocument1.DefaultPageSettings.Margins.Left, YLine, pageWidth + PrintDocument1.DefaultPageSettings.Margins.Left, YLine)

While itm < ListView1.Items.Count
Dim str As String
str = ListView1.Items(itm).Text
e.Graphics.DrawString(str, tablefont, Brushes.Black, X1, Y)
str = ListView1.Items(itm).SubItems(1).Text
Dim R As New RectangleF(X2, Y, W2, 80)
e.Graphics.DrawString(str, tablefont, Brushes.Black, R)
Dim lines, cols As Integer
e.Graphics.MeasureString(str, tablefont, New SizeF(W2, 50), New StringFormat(), cols, lines)
Dim subitm As Integer, Yc As Integer
Yc = Y
For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1
str = ListView1.Items(itm).SubItems(subitm).Text
e.Graphics.DrawString(str, tablefont, Brushes.Black, New RectangleF(X3, Yc, W3, lineheight))
Yc = Yc + tablefont.Height + 2
Next
Y = Y + lines * tablefont.Height + 5
Y = Math.Max(Y, Yc)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Silver, .Margins.Left, Y, pageWidth + .Margins.Left, Y)
If Y > 0.95 * (pageHeight + .Margins.Top) Then
e.HasMorePages = True
drawVerticalLines(e.Graphics, yline, Y)
Exit Sub
End If
End With
itm += 1
subitm += 1
End While
e.HasMorePages = False
drawVerticalLines(e.Graphics, yline, Y)
End Sub
Sub printpagenumber(ByVal G As Graphics)
On Error Resume Next
Dim Xt, Yt As Integer
Xt = PrintDocument1.DefaultPageSettings.Margins.Left
If PrintDocument1.DefaultPageSettings.Landscape Then
Yt = PrintDocument1.DefaultPageSettings.PaperSize.Width - PrintDocument1.DefaultPageSettings.Margins.Bottom + 10
Else
Yt = PrintDocument1.DefaultPageSettings.PaperSize.Heigh t - PrintDocument1.DefaultPageSettings.Margins.Bottom + 10
End If
G.DrawString("Page " & pageno, tablefont, Brushes.Black, Xt, Yt)
End Sub
Sub printReportTitle(ByVal G As Graphics)
Dim Xt, Yt As Single
Xt = PrintDocument1.DefaultPageSettings.Margins.Left
Yt = PrintDocument1.DefaultPageSettings.Margins.Top
G.DrawString(reportTitle, titlefont, Brushes.Black, Xt, Yt)
End Sub

فرید نجفلو
چهارشنبه 17 اسفند 1390, 15:31 عصر
من با راحل مشکل شما فعلا کاری ندارم
ولی شدیدا توصیه ی کنم روش چاپتون رو عوض کنید!
من خودم روی PrintDocument1 خیلی کارکردم ولی بالاخره گذاشتمش کنار
مهمترین دلیلش اینکه گاهی وقتا با نوشته های فارسی مشکل پیدا می کنه
کار کردن هم باهاش سخته(سطح پایین)
اگه اطلاعاتتون به صورت لیستی چاپ می شه و احیانا ادامش می ره تو یه صفحه دیگه برید سراغ کریستال ریپرت
اگه اطلاعاتتون فقط تو یه صفحه چاپ می شه ( مثل فرم ها ، گواهی ها ، برنامه درسی و ...) Reporter ماکروسافت که همراه VS نصب میشه کارتون رو راه میندازه

tstsst
چهارشنبه 17 اسفند 1390, 16:25 عصر
متشکر ولی مشکل این جاست که دیتا بیسی وجود نداره ؟!

فرید نجفلو
چهارشنبه 17 اسفند 1390, 16:49 عصر
شما می تونید اطلاعاتتون رو داخل یک DataTable بریزید و استفاده کنید