|
Thanks for your quick reply Eddie,
Seems like it will work, but it complains:
Object must be of type Int64. This happens during sort.
Any ideas?
Thanks
|
|
|
|
|
Nevermind got it working thanks!
User error!
public class ListViewIPSort: ListViewTextSort
{
public ListViewIPSort(int column, bool ascending):
base(column, ascending)
{
}
protected override int OnCompare(string lhs, string rhs)
{
IPAddress lhsip = IPAddress.Parse(lhs);
IPAddress rhsip = IPAddress.Parse(rhs);
return lhsip.Address.CompareTo(rhsip.Address);
}
}
|
|
|
|
|
Your sorting routine works great but when I try to sort strings that have hyphen - it seems to ignore this character. Example:
Here is my data:
PS-05
PS-10-24
PS2-61P
PS2-61
PS-52
PS6-2
PS5-1
Here is how it sorts:
PS-05
PS-10-24
PS2-61
PS2-61P
PS5-1
PS-52
PS6-2
I would like it to sort like this (by ascii value):
PS-05
PS-10-24
PS-52
PS2-61
PS2-61P
PS5-1
PS6-2
Notice that hyphens are not ignored and are used as part of the sort
Any help would be great
Gerard Iannuzzelli
giannuzzelli@covad.net
|
|
|
|
|
For strings ListViewSortManager just uses String.CompareTo() which performs a word (case-sensitive and culture-sensitive) comparison using the current culture. So if your current culture gives a special meaning to hyphens you'll see this unexpected behavior. If you want to modify this behavior, derive a class from ListViewTextSort and override the OnCompare() method.
Hope this helps,
Eddie
A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine. - Murphy's Law of Computing
|
|
|
|
|
First I'd like to say GREAT JOB Eddie!
Stopping paint also makes column sorting faster..
Works great for me since I'm using different backcolor's on rows...
private void ColumnClick(object sender, ColumnClickEventArgs e)
{
// Faster sorting
m_list.BeginUpdate();
this.Sort(e.Column);
m_list.EndUpdate();
}
id10t
|
|
|
|
|
Thanks! I'm glad to hear that you find this control useful.
I'll try to incorporate your suggestion soon.
A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine. - Murphy's Law of Computing
|
|
|
|
|
Hi,
Can someone pls explain to me the details of this line of code:
ListViewTextSort comp = (ListViewTextSort) Activator.CreateInstance(m_comparers[m_column], ....dadada);
I can kinda make out from some reading that it is a generic constructor caller. But why do we really need it and what is its function?
Basically my problem is I have 2 columns(in future no. of columns will chg programatically). One of the columns is Name and other is Date. But I am kind of treating both of them as simple strings. Hence I am ok with the simplest comparer -viz. ListViewTextSort for both the columns. It does well for me. So I wrote code which looks like:
<br />
m_sortMgr = new ListViewSortManager(my_view,new Type[] {typeof(ListViewTextSort)}, 0,SortOrder.Ascending);<br />
....
....
And my Sort() func in ListViewManager.cs looks like:
<br />
public void Sort(Int32 column, SortOrder order)<br />
{<br />
if(column < 0)
throw new IndexOutOfRangeException();<br />
Also in ListViewSortManager.cs, I commented out all the other (derived)comparers which I am not using.
On doing this sorting works fine for the first column(Name), but whenever I click the second column(Date) I get an exception, that says, index out of bounds, at the above "Activator.CreateInstance()" call.
I figured out the sol'n(not sure if its right) by providing the same comparer 4 times in my def'n to:
<br />
m_sortMgr = new ListViewSortManager(my_view,new Type[] {typeof(ListViewTextSort), typeof(ListViewTextSort),typeof(ListViewTextSort), typeof(ListViewTextSort)},0, SortOrder.Ascending);<br />
With this sorting works fine for both columns. But I am still not clear, how this works.
I have foll questions:
1) Someone pls explain me the detail/need of this Activator.CreateInstance() call and the corresponding 4 entries I have to provide in my workaround def'n of "m_sortMgr" for each comparer, even if all of them are same.
2) In future, when my number of my columns are going to be changing programatically, will I have to assign comparers to them dynamically ? how ?
3) On debugging the sample demo, I never hit the breakpoint for these two funcs - GetColumnComparerType() & SetColumnComparerType(). When/where are these functions used ? their purpose ?
Thanks.
|
|
|
|
|
1) The ListViewSortManager saves the type of the comparers to be used for each column, so the call to Activator.CreateInstance() creates an instance of the particular comparer that will be used by the list view.
2) Assign a new array of comparers with a call to SetComparerTypes() .
3) GetColumnComparerType() and SetColumnComparerType() are helper methods that let you get or set the type of the comparer used by the specified column. The are not used internally by ListViewSortManager , the are intended to be used by the client code if needed.
E_NoName wrote:
m_sortMgr = new ListViewSortManager(my_view,new Type[] {typeof(ListViewTextSort)}, 0,SortOrder.Ascending);
The comparers array needed one entry per-column in the ListView, so this code will only work for single-column lists. To avoid the IndexOutOfRangeException , add one entry in the comparer array for each column.
A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine. - Murphy's Law of Computing
|
|
|
|
|
Thanks Eddie !! Got the hang of it. You got me going. But somehow I have a feeling lurking beneath that I might come back to you later with some more questions Till then..
Cheerio,
-E_NoName
|
|
|
|
|
Thank you very much for the very nice code! Fantastic job! You should become a programmer
|
|
|
|
|
Thank you very much for the very nice code! Fantastic job!
Thanks, and you're welcome.
You should become a programmer
I've been thinking about it.
A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine. - Murphy's Law of Computing
|
|
|
|
|
Hi,
I am creating the Columns dynamically, depending on the return of a database query. And there seems to be a problem, when the Columns are re-created multiple times with the same query there is no problem; but if the new query has a column of different DataType (usually DateTime) then it throws an exception (is using in reallity the previous SortManager although it was re-created with the new Columns). It seems that something has to be cleaned...
Roberto
|
|
|
|
|
You have to reassign the comparers for each column with a call to SetComparerTypes.
If this doesn't help, send me a minimal code sample and I'll see what I can do.
A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine. - Murphy's Law of Computing
|
|
|
|
|
Hi Eddie,
I have an Oracle sql worksheet application where the user enters sql queries and the results are displayed in a listview. Say the user entered:
select username from dba_users
Then I know that one column is returned and after working out the datatype of the column I create the sort manager as follows:
m_sortMgr = new ListViewSortManager(listViewDisplay,
column_sort_type);
where column_sort_type[0] = EV.Windows.Forms.ListViewTextCaseInsensitiveSort
The user then changes the query:
select username,default_tablespace from dba_users
Prior to displaying the data I clear the listViewDisplay and create the sort manager as before:
m_sortMgr = new ListViewSortManager(listViewDisplay,
column_sort_type);
where column_sort_type[0] = EV.Windows.Forms.ListViewTextCaseInsensitiveSort
and column_sort_type[1] = EV.Windows.Forms.ListViewTextCaseInsensitiveSort
When I try and sort on the second column I get a range exception error
in ListViewSortManager
if(column < 0 || column >= m_comparers.Length)
throw new IndexOutOfRangeException();
I tried calling m_sortMgr.SetComparerTypes(column_sort_type);
after creating the sort manager but without success.
Is it possible to initialse or clear the sort manager before the second or subsequent queries?
Otherwise it works really well!
Thanks,
Jim
|
|
|
|
|
Hi guys,
I encountered this EXACT issue and was about to create a new thread. I am doing the same thing - dynamically creating columns based on database queries. I even go as far as destroying the sort manager (objSortMgr = Nothing) but somehow the listview still clings to the previous sort order it was set to.
Was a solution ever found for this problem?
As jwatemail said before: "Otherwise it works really well!"
Thanks in advance...
Rommel the iCeMAn,
Computer Programmer,
Barbados, West Indies.
|
|
|
|
|
Hi Rommel,
I never did get any feedback so I decided to clear the parent tab control and recreate the listview for each query. This may seem rather heavy handed but in practice it works fine. I have about a dozen users working away on a daily basis without problem (well in this bit at least!).
Cheers,
Jim
|
|
|
|
|
Hey,
I've got some good news - I hope!!! . I did some more experimenting and eventually arrived at the following:
<br />
'remove old sort<br />
ListView1.ListViewItemSorter = Nothing<br />
<br />
'implement new sorter - based on your query results<br />
objSortManager.SetComparerTypes(objNewComparer)<br />
<br />
'sort by first column<br />
'<br />
'(when this line is omitted the listview isn't sorted until the user actually clicks a column header)<br />
objSortManager.Sort(0, SortOrder.Ascending)<br />
This worked well for me. I didn't notice any side effects, I was scared at first of "ListView1.ListViewItemSorter = Nothing" as I've never tinkered with that property directly, but I decided to try it anyways.
Hope this works for you as well.
Rommel the iCeMAn,
Computer Programmer,
Barbados, West Indies.
|
|
|
|
|
Hi,
I've successfully compiled your class into a library (EV.Windows.Forms.dll). If added the reference to my VB.NET project but I'm stuck on how to convert the C# declaration.
Here's the C# code (as provided by author):
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
m_sortMgr = new ListViewSortManager(m_list,
new Type[] {
typeof(ListViewTextSort),
typeof(ListViewTextCaseInsensitiveSort),
typeof(ListViewIntegerSort),
typeof(ListViewFloatSort),
typeof(ListViewDateSort)
}
);
}
Here's my VB code:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'
'initialize sorter - this is the line that's giving me stress
oListviewSort = New ListViewSortManager(Listview1, ?????)
Does anyone here know the equivalent VB code?
Thanks in advance
Rommel the iCeMAn,
Computer Programmer,
Barbados, West Indies.
|
|
|
|
|
People, I've found a solution. It's a bit awkward, but it works It appears that C#'s typeof is different to VB.NET's TypeOf. Anyways, here's my code:
'variable declarations
'
'listview sorter
Private oListviewSort As ListViewSortManager
'
'array of comparers (only 2 comparers here because my listview only has 2 columns)
Private oComparers() As System.Type = {New ListViewDateSort(0, True).GetType, New ListViewTextSort(0, True).GetType}
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'
'add sorter to listview
oListviewSort = New ListViewSortManager(lvwLogs, oComparers)
End Sub
<rest of="" code="">
#End Region
After this stunt the listview sorts as expected
iCeMAn out...
Rommel the iCeMAn,
Computer Programmer,
Barbados, West Indies.
|
|
|
|
|
Hi Eddie,
everytime if I fill the listview after sorting it takes long time for filling the listview (many hundred items). After every item the sortmanager sorts the listview new. How can I turn it off? I can start sorting after finnishing the new filling manually, it works much more faster.
Although I set before refilling the sortmanager to null, nevertheless the comparefunction will be executed.
How does it work correctly?
By the way, I changed the typearray for sortingtypes to a arraylist, so I can change my listview dynamically and add or remove sortcolumns. Maybe it's a good feature for your next version?
Ciao, Jimmy
|
|
|
|
|
I found the solution!
Only set listview1.ListViewItemSorter = null!
Thanx!
Ciao, Jimmy
|
|
|
|
|
Could you please explain it more? I also have the same problem.
|
|
|
|
|
Hi Epon
because ListViewSortManager is a external control I thought I have to switch off the ListViewSortManager control for non sorting. But you have to delete the ListViewItemSorter in the ListView control (listView.ListViewItemSorter = null).
Excample:
SortOrder ordertemp = m_sortMgr.SortOrder;<br />
int sortColumn = m_sortMgr.Column;<br />
listView1.ListViewItemSorter = null;<br />
listView1.BeginUpdate();<br />
listView1.Items.Clear();<br />
<br />
... fill listview ... <br />
<br />
if((sortColumn > -1) && (sortColumn < listView1.Columns.Count))<br />
m_sortMgr.Sort(sortColumn,ordertemp); <br />
listView1.EndUpdate();
Ciao, Jimmy S.
|
|
|
|
|
Try using the SortEnabled property of the ListViewSortManager object:
sortManager.SortEnabled = false; Yes, I know, a few days too late!
[EDIT: I just read that property is new as of realease 1.4]
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
I don't know if it's too late for this, but I didn't see where anyone had posted an actual VB.NET version, except for that one that doesn't include the arrow, and seeing as how I'm trying to keep the software on my pc at work 'legal', meaning I do not have C Sharp installed, I decided to convert it. It is a straight conversion, I didn't change anything or add anything.
Here is the url: http://bellsouthpwp.net/j/a/jasonage/ListViewSortManager.zip
|
|
|
|