|
-
May 10th, 2013, 01:34 PM
#1
Thread Starter
Lively Member
[RESOLVED] Need to sort a listview according a combobox choise
Hello. Now I need some help with this little problem.
I have a detailed Listview with 6 columns (headers are hidden), and I need to sort it by any one of its columns which is selected from a combobox.
I have been looking for that, but all the examples I have found are based on a click event on one of the column headers.
Therefore, I am here to thank any help.
Nelson
-
May 10th, 2013, 02:02 PM
#2
Re: Need to sort a listview according a combobox choise
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
May 10th, 2013, 02:13 PM
#3
Re: Need to sort a listview according a combobox choise
here's an example project:
WindowsApplication1.zip
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 04:59 PM
#4
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
Hello MattP and .paul.
Thank you for your replies.
Matt, I have read the article. Very helpful.
Paul, your sample works pretty good.Amaizing how simple it is. However, in my code it returns an error from the Comparer.
The error comes from this line: Return itemX.SubItems(index).Text.CompareTo(itemY.SubItems(index).Text)
It says: InvalidArgument=Value '-1' is not valid for 'index'. (That is a translation, My VB is in Spanish)
I can not figure out where that -1 is coming from.
I am populating that listview from another one, on the run.
Do you think that is the problem?
Thank you all,
Nelson
-
May 10th, 2013, 05:13 PM
#5
Re: Need to sort a listview according a combobox choise
I thought you might have a problem there.
The comparer uses the columnHeader text, (which I set before I removed the columnHeaders) which must be exactly the same as the items listed in the combobox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 11:18 PM
#6
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
Hi paul,
I think the problem is with the method I am using to populate the second listview from the first one. I was using "MYDATA.ListView1.Items.Add(item.Clone())", being Me.listview1 the source.
I am trying to switch to:
HTML Code:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Me.ListView1.FullRowSelect = True
DATA.ListView1.FullRowSelect = True
For Each lvi As ListViewItem In Me.ListView1.SelectedItems
'I do not get to populate the corresponding subitems, they are five.
DATA.ListView1.Items.Add(New ListViewItem(lvi.Text))
Next
End Sub
I would really appreciate your help with this.
Thank you,
Nelson
-
May 11th, 2013, 05:40 AM
#7
Re: Need to sort a listview according a combobox choise
i don't understand what you're doing there. why use the ListView1.SelectedIndexChanged event?
the FullRowSelect would be better placed in the Form_Load event.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DATA.ListView1.Items.AddRange(ListView1.SelectedItems.Cast(Of ListViewItem).Select(Function(lvi) DirectCast(lvi.Clone, ListViewItem)).ToArray)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 11th, 2013, 12:15 PM
#8
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
Hi Paul,
It did it great. Thank you.
About what am I doing?, let me explain:
I have combox1 and combox2 in Form1.
Items in Comboxbox1 are numbers, representing quantities. By clincking on it, user chooses how many items he is going to select from a listview that appears in another form (say Form2).
In that List view, user makes as many choises as Form1.Combobox1 allows. Chosen items and subitems are copyed to a listview that is in Form1. When done, Form2 with its listview is disposed.
Then, by clicking in Form1.combox2 , user selects on which column listview is going to be sorted.
I hope this clears the issue. If it does not, let me know.
I am still having a little problem when sorting a column whose ( I am not sure if I can use "whose" with "It", I am sorry) fields have one or do digits. But, I do not want to abuse. Let me try to fix it by myself.
Thank you so much.
Nelson
-
May 11th, 2013, 12:45 PM
#9
Re: Need to sort a listview according a combobox choise
The problem is in the comparer as numbers (either integers or decimals) don't sort properly with string comparisons, which is what the comparer uses. If you let me know which columns are strings, integers, decimals, or any other datatype I'll rewrite it later.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 11th, 2013, 01:11 PM
#10
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
Paul,
I really do not mean to abuse your kindness, but I am sure I will spend a life on that, since I had to look for VB Forum help after a lot of improductive effort. Really thank you. I owe you a lot!
The Item column is string. The othes, six, are numbers. Three of these, column 2, 3 and 4, could be used to sort the Form1.Listview. As I said they are numbers, they come from an Access .mdb to
Form2.Listview. But, I could change those three columns format to string in the database table, though the data will still be numbers. Let me know If that will be helful.
Thank you,
Nelson
-
May 11th, 2013, 04:11 PM
#11
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
I forgot to tell you, all the numbers have up to 3 integers and always two decimals.
Thak you,
Nelson
-
May 11th, 2013, 11:27 PM
#12
Re: Need to sort a listview according a combobox choise
Code:
Public Class comparer
Implements IComparer
Dim sortBy As String
Dim lvControl As ListView
Public Sub New(ByVal columnName As String, ByVal lv As ListView)
sortBy = columnName
lvControl = lv
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim itemX As ListViewItem = DirectCast(x, ListViewItem)
Dim itemY As ListViewItem = DirectCast(y, ListViewItem)
Dim index As Integer = Array.FindIndex(lvControl.Columns.Cast(Of ColumnHeader).ToArray, Function(ch) ch.Text = sortBy)
Select Case index
Case 0 'column index = 0
'string sorting
Return itemX.SubItems(index).Text.CompareTo(itemY.SubItems(index).Text)
Case Else 'column index <> 0
'decimal sorting
Return CDec(itemX.SubItems(index).Text).CompareTo(CDec(itemY.SubItems(index).Text))
End Select
End Function
End Class
Last edited by .paul.; May 11th, 2013 at 11:30 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 11th, 2013, 11:55 PM
#13
Thread Starter
Lively Member
Re: Need to sort a listview according a combobox choise
Paul,
I did it just great. Thank you so much.
I hope to have a chance to, somehow, return your help.
Nelson
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|