how to print my listview details ..
i found open source with class but this is only printing my 1st column also it is printing 1st column details in all column. please tell me how i can edit it . and put my all subitems of list
the class is with code is below
class
Public Class listViewPrinter
Private lv As ListView
Private location As Point
Private border As Boolean
Private hasGroups As Boolean
Private title As String
Dim titleHeight As Integer
Private WithEvents pd As New Printing.PrintDocument
Public Sub New(ByVal lv As ListView, ByVal location As Point, ByVal border As Boolean, ByVal hasGroups As Boolean, Optional ByVal title As String = "")
Me.lv = lv
Me.location = location
Me.border = border
Me.hasGroups = hasGroups
Me.title = title
titleHeight = If(title <> "", lv.FindForm.CreateGraphics.MeasureString(title, New Font(lv.Font.Name, 25)).ToSize.Height, 0)
End Sub
Public Sub print()
'pd.Print()
Dim ppd As New PrintPreviewDialog
ppd.Document = pd
ppd.WindowState = FormWindowState.Maximized
ppd.ShowDialog()
End Sub
''' <summary>
''' structure to hold printed page details
''' </summary>
''' <remarks></remarks>
Private Structure pageDetails
Dim columns As Integer
Dim rows As Integer
Dim startCol As Integer
Dim startRow As Integer
Dim headerIndices As List(Of Integer)
End Structure
''' <summary>
''' dictionary to hold printed page details, with index key
''' </summary>
''' <remarks></remarks>
Private pages As Dictionary(Of Integer, pageDetails)
Dim maxPagesWide As Integer
Dim maxPagesTall As Integer
Dim items() As ListViewItem
''' <summary>
''' the majority of this Sub is calculating printed page ranges
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
''this removes the printed page margins
pd.OriginAtMargins = True
pd.DefaultPageSettings.Margins = New Drawing.Printing.Margins(location.X, 0, location.Y, 0)
pages = New Dictionary(Of Integer, pageDetails)
Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width) - 40
Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height - (titleHeight + 12)) - 40
Dim pageCounter As Integer = 0
pages.Add(pageCounter, New pageDetails With {.headerIndices = New List(Of Integer)})
Dim columnCounter As Integer = 0
Dim columnSum As Integer = 0
For c As Integer = 0 To lv.Columns.Count - 1
If columnSum + lv.Columns(c).Width < maxWidth Then
columnSum += lv.Columns(c).Width
columnCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
columnSum = lv.Columns(c).Width
columnCounter = 1
pageCounter += 1
pages.Add(pageCounter, New pageDetails With {.startCol = c, .headerIndices = New List(Of Integer)})
End If
If c = lv.Columns.Count - 1 Then
If pages(pageCounter).columns = 0 Then
pages(pageCounter) = New pageDetails With {.columns = columnCounter, .rows = 0, .startCol = pages(pageCounter).startCol, .headerIndices = pages(pageCounter).headerIndices}
End If
End If
Next
maxPagesWide = pages.Keys.Max + 1
pageCounter = 0
Dim rowCounter As Integer = 0
Dim counter As Integer = 0
Dim itemHeight As Integer = lv.GetItemRect(0).Height
Dim rowSum As Integer = itemHeight
If hasGroups Then
For g As Integer = 0 To lv.Groups.Count - 1
rowSum += itemHeight + 6
pages(pageCounter).headerIndices.Add(counter)
For r As Integer = 0 To lv.Groups(g).Items.Count - 1
counter += 1
If rowSum + itemHeight < maxHeight Then
rowSum += itemHeight
rowCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow, .headerIndices = pages(pageCounter).headerIndices}
For x As Integer = 1 To maxPagesWide - 1
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow, .headerIndices = pages(pageCounter).headerIndices}
Next
pageCounter += maxPagesWide
For x As Integer = 0 To maxPagesWide - 1
pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = counter - 1, .headerIndices = New List(Of Integer)})
Next
rowSum = itemHeight * 2 + itemHeight + 6
rowCounter = 1
End If
If counter = lv.Items.Count Then
For x As Integer = 0 To maxPagesWide - 1
If pages(pageCounter + x).rows = 0 Then
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow, .headerIndices = pages(pageCounter + x).headerIndices}
End If
Next
End If
Next
Next
Else
For r As Integer = 0 To lv.Items.Count - 1
counter += 1
If rowSum + itemHeight < maxHeight Then
rowSum += itemHeight
rowCounter += 1
Else
pages(pageCounter) = New pageDetails With {.columns = pages(pageCounter).columns, .rows = rowCounter, .startCol = pages(pageCounter).startCol, .startRow = pages(pageCounter).startRow}
For x As Integer = 1 To maxPagesWide - 1
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter).startRow}
Next
pageCounter += maxPagesWide
For x As Integer = 0 To maxPagesWide - 1
pages.Add(pageCounter + x, New pageDetails With {.columns = pages(x).columns, .rows = 0, .startCol = pages(x).startCol, .startRow = counter - 1})
Next
rowSum = itemHeight * 2
rowCounter = 1
End If
If counter = lv.Items.Count Then
For x As Integer = 0 To maxPagesWide - 1
If pages(pageCounter + x).rows = 0 Then
pages(pageCounter + x) = New pageDetails With {.columns = pages(pageCounter + x).columns, .rows = rowCounter, .startCol = pages(pageCounter + x).startCol, .startRow = pages(pageCounter + x).startRow}
End If
Next
End If
Next
End If
maxPagesTall = pages.Count \ maxPagesWide
If hasGroups Then
items = New ListViewItem() {}
For Each g As ListViewGroup In lv.Groups
items = items.Concat(g.Items.Cast(Of ListViewItem).ToArray).ToArray
Next
Else
items = lv.Items.Cast(Of ListViewItem).ToArray
End If
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Dim r2 As New Rectangle(location, New Size(lv.Columns.Cast(Of ColumnHeader).Skip(pages(0).startCol).Take(pages(0).columns).Sum(Function(ch As ColumnHeader) ch.Width), titleHeight))
e.Graphics.DrawString(title, New Font(lv.Font.Name, 25), Brushes.Black, r2, sf)
sf.Alignment = StringAlignment.Near
Dim startX As Integer = location.X
Dim startY As Integer = location.Y + titleHeight + 12
Static startPage As Integer = 0
Dim itemHeight As Integer = lv.GetItemRect(0).Height
Dim bottomRight As Point
For p As Integer = startPage To pages.Count - 1
startX = location.X
startY = location.Y + titleHeight + 12
Dim cell As Rectangle
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
e.Graphics.DrawRectangle(Pens.Black, cell)
e.Graphics.DrawString(lv.Columns(c).Text, lv.Font, Brushes.Black, cell, sf)
startX += lv.Columns(c).Width
Next
startY += itemHeight
startX = location.X
For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
startX = location.X
If hasGroups Then
If r = pages(p).startRow Or pages(p).headerIndices.Contains(r) Then
cell = New Rectangle(startX + 20, startY + 6, lv.Columns.Cast(Of ColumnHeader).Skip(pages(p).startCol).Take(pages(p).columns).Sum(Function(ch As ColumnHeader) ch.Width), itemHeight)
e.Graphics.DrawString(items(r).Group.Header, New Font(lv.Font, FontStyle.Bold), Brushes.SteelBlue, cell, sf)
e.Graphics.DrawLine(New Pen(Color.SteelBlue, 2), startX + 20 + e.Graphics.MeasureString(items(r).Group.Header, New Font(lv.Font, FontStyle.Bold)).Width + 12, cell.Top + 3 + cell.Height \ 2, cell.Right - 20, cell.Top + 3 + cell.Height \ 2)
startY += itemHeight + 6
End If
End If
For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
cell = New Rectangle(startX, startY, lv.Columns(c).Width, itemHeight)
**e.Graphics.DrawString(items(r).SubItems(c).Text, lv.Font, Brushes.Black, cell, sf)**
If lv.GridLines Then e.Graphics.DrawRectangle(Pens.Black, cell)
startX += lv.Columns(c).Width
Next
startY += itemHeight
If r = pages(p).startRow + pages(p).rows - 1 Then
bottomRight = New Point(startX, startY)
If border Then e.Graphics.DrawRectangle(Pens.Black, New Rectangle(location, New Size(bottomRight.X - location.X, bottomRight.Y - location.Y)))
End If
Next
If p <> pages.Count - 1 Then
startPage = p + 1
e.HasMorePages = True
Return
Else
startPage = 0
End If
Next
End Sub
End Class
form1 code
Public Class Form1
Dim groupNames() As String = {"Group1", "Group2", "Group3"}
Dim randomObject As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For r As Integer = 1 To 250
Dim randomGroupName As String = groupNames(randomObject.Next(0, 3))
Dim group As New ListViewGroup(randomGroupName, randomGroupName)
If Not ListView1.Groups.Cast(Of ListViewGroup).Any(Function(lvg) lvg.Name = randomGroupName) Then
ListView1.Groups.Add(group)
End If
Dim item As ListViewItem = ListView1.Items.Add( _
New ListViewItem(New String() {String.Format("A{0}", r), _
String.Format("B{0}", r), _
String.Format("C{0}", r), _
String.Format("D{0}", r), _
String.Format("E{0}", r), _
String.Format("F{0}", r), _
String.Format("G{0}", r), _
String.Format("H{0}", r), _
String.Format("I{0}", r), _
String.Format("J{0}", r), _
String.Format("K{0}", r), _
String.Format("L{0}", r), _
String.Format("M{0}", r), _
String.Format("N{0}", r), _
String.Format("O{0}", r), _
String.Format("P{0}", r), _
String.Format("Q{0}", r), _
String.Format("R{0}", r), _
String.Format("S{0}", r), _
String.Format("T{0}", r), _
String.Format("U{0}", r), _
String.Format("V{0}", r), _
String.Format("W{0}", r), _
String.Format("X{0}", r), _
String.Format("Y{0}", r), _
String.Format("Z{0}", r)}))
ListView1.Groups.Cast(Of ListViewGroup).First(Function(lvg) lvg.Name = randomGroupName).Items.Add(item)
Next
Dim listGroups As List(Of ListViewGroup) = ListView1.Groups.Cast(Of ListViewGroup).ToList
listGroups.Sort(New groupSorter)
ListView1.Groups.Clear()
ListView1.Groups.AddRange(listGroups.ToArray)
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
For x As Integer = 0 To ListView1.Columns.Count - 1
ListView1.Columns(x).Width += 12
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim printer As New listViewPrinter(ListView1, New Point(50, 50), chkBorder.Checked, ListView1.Groups.Count > 0, "titleText")
printer.print()
End Sub
Private Sub chkGridLines_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGridLines.CheckedChanged
ListView1.GridLines = chkGridLines.Checked
End Sub
End Class
Public Class groupSorter
Implements IComparer(Of ListViewGroup)
Public Function Compare(ByVal x As System.Windows.Forms.ListViewGroup, ByVal y As System.Windows.Forms.ListViewGroup) As Integer Implements System.Collections.Generic.IComparer(Of System.Windows.Forms.ListViewGroup).Compare
Return x.Name.CompareTo(y.Name)
End Function
End Class
this code is working fine but for me when i add it in my project it replace all columns with my 1st columns details and print it same way...i dont know how to add all columns..