digitaldrew
Well-known member
- Joined
- Nov 10, 2012
- Messages
- 167
- Programming Experience
- Beginner
Hello everyone.. I have a listview with a variety of columns in it along with the option to export everything to CSV. The list fills up just fine and exporting it to CSV previously worked fine, until I added the ability to Sort the colums. Now, anytime a column is clicked (displaying a "<" or "<" for ascending/descending next to the header text) the < or > symbol gets it's own header column, messing up all the data in the listview matching the column headers..
This is the code for sorting the column
This is the code for exporting the listview to CSV
Any idea what could be changed so "<" and ">" don't get a column inside the CSV and instead appear next to the column text which is sorted.
Thanks in advance!
This is the code for sorting the column
VB.NET:
' Get the new sorting column.
Dim new_sorting_column As ColumnHeader = lstDomainDetails.Columns(e.Column)
' Figure out the new sorting order.
Dim sort_order As System.Windows.Forms.SortOrder
If m_SortingColumn Is Nothing Then
' New column. Sort ascending.
sort_order = SortOrder.Ascending
Else ' See if this is the same column.
If new_sorting_column.Equals(m_SortingColumn) Then
' Same column. Switch the sort order.
If m_SortingColumn.Text.StartsWith("> ") Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
Else
' New column. Sort ascending.
sort_order = SortOrder.Ascending
End If
' Remove the old sort indicator.
m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
End If
' Display the new sort order.
m_SortingColumn = new_sorting_column
If sort_order = SortOrder.Ascending Then
m_SortingColumn.Text = "> " & m_SortingColumn.Text
Else
m_SortingColumn.Text = "< " & m_SortingColumn.Text
End If
' Create a comparer.
lstDomainDetails.ListViewItemSorter = New clsListviewSorter(e.Column, sort_order)
' Sort.
lstDomainDetails.Sort()
This is the code for exporting the listview to CSV
VB.NET:
Dim sfd As New SaveFileDialog
sfd.InitialDirectory = "C:/"
sfd.FileName = "DomainData.csv"
sfd.Title = "Export Domain Data"
sfd.Filter = ("Text Files (*.csv) | *.csv")
sfd.FilterIndex = 0
'show the dialog + display the results in a msgbox unless cancelled
If sfd.ShowDialog = DialogResult.OK Then
Dim headers = (From ch In lstDomainDetails.Columns _
Let header = DirectCast(ch, ColumnHeader) _
Select header.Text).ToArray()
Dim items() = (From item In lstDomainDetails.Items _
Let lvi = DirectCast(item, ListViewItem) _
Select (From subitem In lvi.SubItems _
Let si = DirectCast(subitem, ListViewItem.ListViewSubItem) _
Select si.Text).ToArray()).ToArray()
Dim table As String = String.Join(",", headers) & Environment.NewLine
For Each a In items
table &= String.Join(",", a) & Environment.NewLine
Next
table = table.TrimEnd(CChar(vbCr), CChar(vbLf))
IO.File.WriteAllText(sfd.FileName, table)
MsgBox("Domain Data Successfully Saved")
End If
Any idea what could be changed so "<" and ">" don't get a column inside the CSV and instead appear next to the column text which is sorted.
Thanks in advance!
Last edited: