473,574 Members | 18,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you see why this does not sort the list?

Can you see why this does not sort the list?

It displays OK but is not sorted

Thanks for any help

Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInteg er)

'StringWithInte ger has properties Strng and Numb

....

GenericComboBox 1.BindingSource DataSource = mItemList

GenericComboBox 1.ItemDisplayMe mber = "Strng"

GenericComboBox 1.ItemValueMemb er = "Numb"

GenericComboBox 1.SortItNow("St rng ASC")

GenericComboBox 1.SetDataSource ()



Public Class GenericComboBox

Inherits System.Windows. Forms.ComboBox

#Region " Windows Form Designer generated code "

....

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDa taSource() As Object

Set(ByVal itemList As Object)

BindingSource1. DataSource = itemList

End Set

End Property

Public Sub SetDataSource()

Me.DataSource = BindingSource1

End Sub

Public Sub SortItNow(ByVal sortString As String)

BindingSource1. Sort = sortString

End Sub

End Class
Mar 24 '07 #1
9 1993
When you bind a control, what's in the database is the way you're going to
see the data. However, you could simply enumerate through the field values
and put them into the list and then sort. Or you can simply make the call to
the database for that specific field and then specify ASC .
" active" <ac********** @a-znet.comwrote in message
news:eR******** ******@TK2MSFTN GP03.phx.gbl...
Can you see why this does not sort the list?

It displays OK but is not sorted

Thanks for any help

Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInteg er)

'StringWithInte ger has properties Strng and Numb

...

GenericComboBox 1.BindingSource DataSource = mItemList

GenericComboBox 1.ItemDisplayMe mber = "Strng"

GenericComboBox 1.ItemValueMemb er = "Numb"

GenericComboBox 1.SortItNow("St rng ASC")

GenericComboBox 1.SetDataSource ()



Public Class GenericComboBox

Inherits System.Windows. Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDa taSource() As Object

Set(ByVal itemList As Object)

BindingSource1. DataSource = itemList

End Set

End Property

Public Sub SetDataSource()

Me.DataSource = BindingSource1

End Sub

Public Sub SortItNow(ByVal sortString As String)

BindingSource1. Sort = sortString

End Sub

End Class

Mar 25 '07 #2

"Richard T. Edwards" <re********@hot mail.comwrote in message
news:ux******** ******@TK2MSFTN GP02.phx.gbl...
When you bind a control, what's in the database is the way you're going to
see the data. However, you could simply enumerate through the field values
and put them into the list and then sort.
Does this mean to get each item of mItemList and add it to the
BindingSource.
Even tho mItemList is already bound to it?
I just don't understand the db terminology.

>Or you can simply make the call to the database for that specific field and
then specify ASC .
Could you just give me a little more, maybe a partial example.

I'd appreciate it
Thanks
>

" active" <ac********** @a-znet.comwrote in message
news:eR******** ******@TK2MSFTN GP03.phx.gbl...
>Can you see why this does not sort the list?

It displays OK but is not sorted

Thanks for any help

Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInte ger)

'StringWithInt eger has properties Strng and Numb

...

GenericComboBo x1.BindingSourc eDataSource = mItemList

GenericComboBo x1.ItemDisplayM ember = "Strng"

GenericComboBo x1.ItemValueMem ber = "Numb"

GenericComboBo x1.SortItNow("S trng ASC")

GenericComboBo x1.SetDataSourc e()



Public Class GenericComboBox

Inherits System.Windows. Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDa taSource() As Object

Set(ByVal itemList As Object)

BindingSource1 .DataSource = itemList

End Set

End Property

Public Sub SetDataSource()

Me.DataSourc e = BindingSource1

End Sub

Public Sub SortItNow(ByVal sortString As String)

BindingSource1 .Sort = sortString

End Sub

End Class


Mar 25 '07 #3
Are you using the ComboBox control only for the sorting? Then you should
just make a custom comparer for the StringWithInteg er class, and use the
Sort method of the List class.

Something like this (my VB is a bit rusty):

Public Class StringWithInteg erComparer Inherits IComparer(Of
StringWithInteg er)

Public Function Compare(StringW ithInteger a, StringWithInteg er b) As
Integer
Return String.Compare( a.Strng, b.Strng)
End Function

End Class

Dim StringWithInteg erComparer comparer = New StringWithInteg erComparer()
mItemList.Sort( comparer)

If you need to alter the sorting, you can add that functionalty to the
comparer, or just create another one.

By the way, you might want to reconsider your naming strategies.
Although StringWithInteg er clearly says what the class is, it's a bit
clumsy, and you might want to name it after what it's supposed to be
used for. A common reccomendation is to avoid data types in variable
names altogehter. Also, misspelled names like Strng are always difficult
to work with. (I once worked with a database where half of the time view
was spelled wiev...)

active wrote:
Can you see why this does not sort the list?

It displays OK but is not sorted

Thanks for any help

Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInteg er)

'StringWithInte ger has properties Strng and Numb

...

GenericComboBox 1.BindingSource DataSource = mItemList

GenericComboBox 1.ItemDisplayMe mber = "Strng"

GenericComboBox 1.ItemValueMemb er = "Numb"

GenericComboBox 1.SortItNow("St rng ASC")

GenericComboBox 1.SetDataSource ()



Public Class GenericComboBox

Inherits System.Windows. Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDa taSource() As Object

Set(ByVal itemList As Object)

BindingSource1. DataSource = itemList

End Set

End Property

Public Sub SetDataSource()

Me.DataSource = BindingSource1

End Sub

Public Sub SortItNow(ByVal sortString As String)

BindingSource1. Sort = sortString

End Sub

End Class


--
Göran Andersson
_____
http://www.guffa.com
Mar 25 '07 #4

As to: making a comparer:
I did it this way mainly because I wanted to use bound data which I've never
used before.
And in fact can make it work with the SortedList class but can't figure out
why I can't make it work with the List class.
As to:
"misspelled names like Strng are always difficult to work with"
I agree except that I have a few that are always spelt the same:
Numb for example - never use Number, always Numb
Since they are always spelt the same I never get into trouble because of
them.

Thanks a lot
"Göran Andersson" <gu***@guffa.co mwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Are you using the ComboBox control only for the sorting? Then you should
just make a custom comparer for the StringWithInteg er class, and use the
Sort method of the List class.

Something like this (my VB is a bit rusty):

Public Class StringWithInteg erComparer Inherits IComparer(Of
StringWithInteg er)

Public Function Compare(StringW ithInteger a, StringWithInteg er b) As
Integer
Return String.Compare( a.Strng, b.Strng)
End Function

End Class

Dim StringWithInteg erComparer comparer = New StringWithInteg erComparer()
mItemList.Sort( comparer)

If you need to alter the sorting, you can add that functionalty to the
comparer, or just create another one.

By the way, you might want to reconsider your naming strategies. Although
StringWithInteg er clearly says what the class is, it's a bit clumsy, and
you might want to name it after what it's supposed to be used for. A
common reccomendation is to avoid data types in variable names altogehter.
Also, misspelled names like Strng are always difficult to work with. (I
once worked with a database where half of the time view was spelled
wiev...)

active wrote:
>Can you see why this does not sort the list?

It displays OK but is not sorted

Thanks for any help

Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInte ger)

'StringWithInt eger has properties Strng and Numb

...

GenericComboBo x1.BindingSourc eDataSource = mItemList

GenericComboBo x1.ItemDisplayM ember = "Strng"

GenericComboBo x1.ItemValueMem ber = "Numb"

GenericComboBo x1.SortItNow("S trng ASC")

GenericComboBo x1.SetDataSourc e()



Public Class GenericComboBox

Inherits System.Windows. Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDa taSource() As Object

Set(ByVal itemList As Object)

BindingSource1 .DataSource = itemList

End Set

End Property

Public Sub SetDataSource()

Me.DataSourc e = BindingSource1

End Sub

Public Sub SortItNow(ByVal sortString As String)

BindingSource1 .Sort = sortString

End Sub

End Class



--
Göran Andersson
_____
http://www.guffa.com

Mar 25 '07 #5
active wrote:
Can you see why this does not sort the list?
It displays OK but is not sorted
<snip>
Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInteg er)
<snip>
GenericComboBox 1.BindingSource DataSource = mItemList
GenericComboBox 1.SortItNow("St rng ASC")
<snip>
Public Class GenericComboBox
Inherits System.Windows. Forms.ComboBox
<snip>
Public BindingSource1 As BindingSource = New BindingSource()
<snip>
Public Sub SortItNow(ByVal sortString As String)
BindingSource1. Sort = sortString
End Sub
End Class
According to the docs, the BindingSource class relies on the objectc
assigned as DataSource to most of its operations, which most of the
times requires only an IList implemtation form the DataSource. But for
the all-important operation of sorting, BindingSource requires that
the DataSource implements IBindingList or IBindingListVie w.

The problem with most general list implementations provided by the
framework (List(Of T), Dictionary, ArrayList, etc etc) is that none of
them supports IBindingList or IBindingListVie w (even though some of
then do support sorting, relying usually on the contained data
supporting IComparable, or taking hold of an instance of IComparer).

You'll find an *almost* complete implementation of IBindingList in the
System.Componen tModel.BindingL ist(Of T) class. Unfortuantely this
class *doesn't* support sorting either -- you'd have to derive from it
and implement sorting yourself by overriding the ApplySortCore,
RemoveSortCore and SupportsSorting Core methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.Dat aView. Unfortunately (again),
DataViews can only deal with DataTables, not with List(of), Arrays,
etc...

Sorry for the bad news.

Regards,

Branco

PS: For quick and dirty tests, using Public fields in your class
*seems* OK. It's not. Public fields in a class are exactly that: a
public field, acessible by whomever gets a reference to an instance of
the class. It *is not* protected by underlying get_Field/set_Field
methods as you could suppose (if you were a hardcore VB6/5/4
programmer in a previous lifetime, I mean). If you later on decide --
and you should -- to change that field access to a property access,
then the interface of you class *will change* (code that relied on
accessing the field will no longer work). Not really a problem if your
class is used only inside that particular project, but a bad practice
(IM*X*HO) nonetheless (worse still if the class will be used as a
visual component)... Just a 'heads up'.
Mar 26 '07 #6

"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ d57g2000hsg.goo glegroups.com.. .
active wrote:
>Can you see why this does not sort the list?
It displays OK but is not sorted
<snip>
>Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInte ger)
<snip>
>GenericComboBo x1.BindingSourc eDataSource = mItemList
GenericComboBo x1.SortItNow("S trng ASC")
<snip>
>Public Class GenericComboBox
Inherits System.Windows. Forms.ComboBox
<snip>
>Public BindingSource1 As BindingSource = New BindingSource()
<snip>
>Public Sub SortItNow(ByVal sortString As String)
BindingSource1 .Sort = sortString
End Sub
End Class

According to the docs, the BindingSource class relies on the objectc
assigned as DataSource to most of its operations, which most of the
times requires only an IList implemtation form the DataSource. But for
the all-important operation of sorting, BindingSource requires that
the DataSource implements IBindingList or IBindingListVie w.

The problem with most general list implementations provided by the
framework (List(Of T), Dictionary, ArrayList, etc etc) is that none of
them supports IBindingList or IBindingListVie w (even though some of
then do support sorting, relying usually on the contained data
supporting IComparable, or taking hold of an instance of IComparer).

You'll find an *almost* complete implementation of IBindingList in the
System.Componen tModel.BindingL ist(Of T) class. Unfortuantely this
class *doesn't* support sorting either -- you'd have to derive from it
and implement sorting yourself by overriding the ApplySortCore,
RemoveSortCore and SupportsSorting Core methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.Dat aView. Unfortunately (again),
DataViews can only deal with DataTables, not with List(of), Arrays,
etc...

Sorry for the bad news.

Regards,

Branco

PS: For quick and dirty tests, using Public fields in your class
*seems* OK. It's not. Public fields in a class are exactly that: a
public field, acessible by whomever gets a reference to an instance of
the class. It *is not* protected by underlying get_Field/set_Field
methods as you could suppose (if you were a hardcore VB6/5/4
programmer in a previous lifetime, I mean). If you later on decide --
and you should -- to change that field access to a property access,
then the interface of you class *will change* (code that relied on
accessing the field will no longer work). Not really a problem if your
class is used only inside that particular project, but a bad practice
(IM*X*HO) nonetheless (worse still if the class will be used as a
visual component)... Just a 'heads up'.

On the bright side, Brian Noyes shows how to implement the sort in the
BindingList(Of T) type of implementation. And if you looked up his book and
then googled him, maybe you could find the source code from the book...

Robin S.
Mar 27 '07 #7
Thanks for such a complete explanation
"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ d57g2000hsg.goo glegroups.com.. .
active wrote:
>Can you see why this does not sort the list?
It displays OK but is not sorted
<snip>
>Private mItemList As List(Of StringWithInteg er) = New List(Of
StringWithInte ger)
<snip>
>GenericComboBo x1.BindingSourc eDataSource = mItemList
GenericComboBo x1.SortItNow("S trng ASC")
<snip>
>Public Class GenericComboBox
Inherits System.Windows. Forms.ComboBox
<snip>
>Public BindingSource1 As BindingSource = New BindingSource()
<snip>
>Public Sub SortItNow(ByVal sortString As String)
BindingSource1 .Sort = sortString
End Sub
End Class

According to the docs, the BindingSource class relies on the objectc
assigned as DataSource to most of its operations, which most of the
times requires only an IList implemtation form the DataSource. But for
the all-important operation of sorting, BindingSource requires that
the DataSource implements IBindingList or IBindingListVie w.

The problem with most general list implementations provided by the
framework (List(Of T), Dictionary, ArrayList, etc etc) is that none of
them supports IBindingList or IBindingListVie w (even though some of
then do support sorting, relying usually on the contained data
supporting IComparable, or taking hold of an instance of IComparer).

You'll find an *almost* complete implementation of IBindingList in the
System.Componen tModel.BindingL ist(Of T) class. Unfortuantely this
class *doesn't* support sorting either -- you'd have to derive from it
and implement sorting yourself by overriding the ApplySortCore,
RemoveSortCore and SupportsSorting Core methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.Dat aView. Unfortunately (again),
DataViews can only deal with DataTables, not with List(of), Arrays,
etc...

Sorry for the bad news.

Regards,

Branco

PS: For quick and dirty tests, using Public fields in your class
*seems* OK. It's not. Public fields in a class are exactly that: a
public field, acessible by whomever gets a reference to an instance of
the class. It *is not* protected by underlying get_Field/set_Field
methods as you could suppose (if you were a hardcore VB6/5/4
programmer in a previous lifetime, I mean). If you later on decide --
and you should -- to change that field access to a property access,
then the interface of you class *will change* (code that relied on
accessing the field will no longer work). Not really a problem if your
class is used only inside that particular project, but a bad practice
(IM*X*HO) nonetheless (worse still if the class will be used as a
visual component)... Just a 'heads up'.


Mar 27 '07 #8

Unless you tell me it's not a good idea I'll continue to use SortedList
which appears to work
Thanks
On the bright side, Brian Noyes shows how to implement the sort in the
BindingList(Of T) type of implementation. And if you looked up his book
and then googled him, maybe you could find the source code from the
book...

Robin S.

Mar 27 '07 #9
If it works for you, go for it. :-)

Robin S.
---------------------
" active" <ac********** @a-znet.comwrote in message
news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
>
Unless you tell me it's not a good idea I'll continue to use SortedList
which appears to work
Thanks
>On the bright side, Brian Noyes shows how to implement the sort in the
BindingList( Of T) type of implementation. And if you looked up his book
and then googled him, maybe you could find the source code from the
book...

Robin S.


Mar 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2560
by: Steve Pinard | last post by:
(Got a comm error trying to post first time, sorry if this is a duplicate) New to Python, so please bear with me. >>> import sys >>> print sys.modules.keys() # works fine >>> print sys.modules.keys().sort() # returns None, why? None
3
1618
by: Brian McGonigle | last post by:
I'm a Perl programmer learning Python (up to chapter 7 in Learning Python, so go easy on me :-) and I find that I look to do things in Python the way I would do them in Perl. In Perl functions and methods usually only return and undefined value in the event of an error, make an endless number of compound statements possible. Is there a version...
12
2907
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an "Aborted(core
2
4623
by: Tim Marshall | last post by:
The following is happening in two instances in A2003. The one I s\describe is the easiest one. Theme controls and autocorrupt have been turned off long ago. An unbound main form with a list box (query row source and multi-select turned off) and a datasheet subform. The short cut menu for the datasheet subform includes standard sort...
1
12831
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
3
2698
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h> #include<stdlib.h> int main(void) {
11
1937
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them to the database. How can accomplish this task? Thanks!
99
4613
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
3
10546
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you...
0
8081
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8258
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8119
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5322
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.