How to sort listview according to a specific column

Asked

Viewed 2,312 times

0

I have a listview that shows more or less the following:

ID    NOME    IDADE
0     Luis     19
1     Julio    33
2     Marcio   27

The problem is that when the ID comes in at 10, the listview starts to put the 11, 12, 13... before the 2, getting something like:

ID    NOME     IDADE
0     Luis      19
1     Julio     33
11    Claudio   33
12    Felipe    33
2     Marcio    27

I wanted to know how to fix this by making 11, 12, 13... come after 10, and so on.

I recently discovered that the problem lies in the code that reads the files(the information is taken from XML files)

Dim DirDiretorio As DirectoryInfo = New DirectoryInfo(pedidosPath)
Dim oFileInfoCollection() As FileInfo
Dim oFileInfo As FileInfo
Dim i As Integer
oFileInfoCollection = DirDiretorio.GetFiles()
For i = 0 To oFileInfoCollection.Length() - 1
    oFileInfo = oFileInfoCollection.GetValue(i)
    'Pega os dados do arquivo
Next

With a Messagebox I discovered that he takes the 10, 11, 12, ... before the 2. Some alternative code?

  • I don’t know much about Vb, but are you treating this column as a whole (if that’s how Vb sees types)? I’ve seen similar problems in other languages, and in the end, the problem was the type of treatment.

  • In vedade, this list is much bigger, there are 7 subitens, this example I put was very crude. The program adds values from XML files, that is, each item (ID, NAME, AGE) is within an XML. I didn’t quite understand your question, but I add the entire item from one file only.

  • I think I was a little confused, I asked you if you treat the column id like int(integer numbers). In other languages, this problem was caused because the column ordering was wrong because the program read as string.

  • Oh yes, I get it. it’s like String, since I add all the values of all the columns at the same time. lstPedidos.Items.Add(New ListViewItem(New String() {"", id, nome, telefone,...}))

  • It would be interesting for you to add in the question how you populate the listview with the data. Apparently it’s a problem with data types, as I said.

  • Thanks in advance!!

Show 1 more comment

1 answer

-1

Just use the property Listviewitemsorter of Listview, to sort by a specific column. In the example below I am sorting by the first column.

Dim ListView1 As New ListView
Dim lvwColumnSorter As ListViewColumnSorter

lvwColumnSorter = New ListViewColumnSorter()

lvwColumnSorter.SortColumn = ListView1.Columns(0).Index
lvwColumnSorter.Order = SortOrder.Ascending

ListView1.ListViewItemSorter = lvwColumnSorter

ListView1.Sort()

in the links below there is more information about column ordering in Listview.

https://support.microsoft.com/pt-br/kb/319399

https://msdn.microsoft.com/pt-br/library/system.windows.forms.listview.listviewitemsorter(v=vs.110). aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.