Click here to Skip to main content
15,891,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm populating a listview based on selected records in a datagridview. I want to sort the listview items (ascending) but somehow I keep getting 1,10,4,5 instead of 1,4,5,10. I am using CInt to convert the items to integer but this doesn't help.

Can anyone point me in the right direction?
Many thanks,
Eric

VB
Dim selectedRowCount As Integer = frmData.DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)

        If selectedRowCount > 0 Then
            Dim i As Integer
            For i = 0 To selectedRowCount - 1
                ListView1.Items.Add(CInt(frmData.DataGridView1.SelectedRows(i).Cells(0).Value))
            Next i

            ListView1.Sorting = SortOrder.Ascending
        End If



[edit] pre lang sql changed to vb [/edit]
Posted
Updated 26-May-12 11:30am
v2

See here[^]; the default is alphabetical order, so 10 comes before 4.
 
Share this answer
 
Comments
Maciej Los 1-Jun-12 15:39pm    
Good answer, my 5!
If u want something different then the default order routine.
U can implement an own sorting routine by implementing IComparer which u next supply to the list (u can pass it as an extra parameter in the Sort routine).

If u get the listviewitem in a local array, then remove them from the listview, the sort them your way in a list, then readd them to the listview, well then it' sorted your way. U need to disable sorting on the listview for this.
 
Share this answer
 
v3
Comments
CPallini 26-May-12 12:21pm    
What is 'u'?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900