By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,396 Members | 2,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,396 IT Pros & Developers. It's quick & easy.

Can you see why this does not sort the list?

P: n/a
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 StringWithInteger) = New List(Of
StringWithInteger)

'StringWithInteger has properties Strng and Numb

....

GenericComboBox1.BindingSourceDataSource = mItemList

GenericComboBox1.ItemDisplayMember = "Strng"

GenericComboBox1.ItemValueMember = "Numb"

GenericComboBox1.SortItNow("Strng ASC")

GenericComboBox1.SetDataSource()



Public Class GenericComboBox

Inherits System.Windows.Forms.ComboBox

#Region " Windows Form Designer generated code "

....

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDataSource() 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
Share this Question
Share on Google+
9 Replies


P: n/a
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**************@TK2MSFTNGP03.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 StringWithInteger) = New List(Of
StringWithInteger)

'StringWithInteger has properties Strng and Numb

...

GenericComboBox1.BindingSourceDataSource = mItemList

GenericComboBox1.ItemDisplayMember = "Strng"

GenericComboBox1.ItemValueMember = "Numb"

GenericComboBox1.SortItNow("Strng ASC")

GenericComboBox1.SetDataSource()



Public Class GenericComboBox

Inherits System.Windows.Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDataSource() 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

P: n/a

"Richard T. Edwards" <re********@hotmail.comwrote in message
news:ux**************@TK2MSFTNGP02.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**************@TK2MSFTNGP03.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 StringWithInteger) = New List(Of
StringWithInteger)

'StringWithInteger has properties Strng and Numb

...

GenericComboBox1.BindingSourceDataSource = mItemList

GenericComboBox1.ItemDisplayMember = "Strng"

GenericComboBox1.ItemValueMember = "Numb"

GenericComboBox1.SortItNow("Strng ASC")

GenericComboBox1.SetDataSource()



Public Class GenericComboBox

Inherits System.Windows.Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDataSource() 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 #3

P: n/a
Are you using the ComboBox control only for the sorting? Then you should
just make a custom comparer for the StringWithInteger class, and use the
Sort method of the List class.

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

Public Class StringWithIntegerComparer Inherits IComparer(Of
StringWithInteger)

Public Function Compare(StringWithInteger a, StringWithInteger b) As
Integer
Return String.Compare(a.Strng, b.Strng)
End Function

End Class

Dim StringWithIntegerComparer comparer = New StringWithIntegerComparer()
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 StringWithInteger 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 StringWithInteger) = New List(Of
StringWithInteger)

'StringWithInteger has properties Strng and Numb

...

GenericComboBox1.BindingSourceDataSource = mItemList

GenericComboBox1.ItemDisplayMember = "Strng"

GenericComboBox1.ItemValueMember = "Numb"

GenericComboBox1.SortItNow("Strng ASC")

GenericComboBox1.SetDataSource()



Public Class GenericComboBox

Inherits System.Windows.Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDataSource() 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

P: n/a

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.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Are you using the ComboBox control only for the sorting? Then you should
just make a custom comparer for the StringWithInteger class, and use the
Sort method of the List class.

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

Public Class StringWithIntegerComparer Inherits IComparer(Of
StringWithInteger)

Public Function Compare(StringWithInteger a, StringWithInteger b) As
Integer
Return String.Compare(a.Strng, b.Strng)
End Function

End Class

Dim StringWithIntegerComparer comparer = New StringWithIntegerComparer()
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
StringWithInteger 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 StringWithInteger) = New List(Of
StringWithInteger)

'StringWithInteger has properties Strng and Numb

...

GenericComboBox1.BindingSourceDataSource = mItemList

GenericComboBox1.ItemDisplayMember = "Strng"

GenericComboBox1.ItemValueMember = "Numb"

GenericComboBox1.SortItNow("Strng ASC")

GenericComboBox1.SetDataSource()



Public Class GenericComboBox

Inherits System.Windows.Forms.ComboBox

#Region " Windows Form Designer generated code "

...

Public BindingSource1 As BindingSource = New BindingSource()

Public WriteOnly Property BindingSourceDataSource() 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 #5

P: n/a
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 StringWithInteger) = New List(Of
StringWithInteger)
<snip>
GenericComboBox1.BindingSourceDataSource = mItemList
GenericComboBox1.SortItNow("Strng 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 IBindingListView.

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 IBindingListView (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.ComponentModel.BindingList(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 SupportsSortingCore methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.DataView. 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

P: n/a

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.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 StringWithInteger) = New List(Of
StringWithInteger)
<snip>
>GenericComboBox1.BindingSourceDataSource = mItemList
GenericComboBox1.SortItNow("Strng 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 IBindingListView.

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 IBindingListView (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.ComponentModel.BindingList(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 SupportsSortingCore methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.DataView. 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

P: n/a
Thanks for such a complete explanation
"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.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 StringWithInteger) = New List(Of
StringWithInteger)
<snip>
>GenericComboBox1.BindingSourceDataSource = mItemList
GenericComboBox1.SortItNow("Strng 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 IBindingListView.

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 IBindingListView (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.ComponentModel.BindingList(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 SupportsSortingCore methods. *yuck!*

One class that seems to provide a complete implementation of the
IBindingList interface is System.Data.DataView. 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

P: n/a

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

P: n/a
If it works for you, go for it. :-)

Robin S.
---------------------
" active" <ac**********@a-znet.comwrote in message
news:uZ**************@TK2MSFTNGP03.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 discussion thread is closed

Replies have been disabled for this discussion.