Can you see why this does not sort the list?

A

active

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
 
A

active

Richard T. Edwards said:
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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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...)
 
A

active

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
 
B

Branco Medeiros

active said:
Can you see why this does not sort the list?
It displays OK but is not sorted
Private mItemList As List(Of StringWithInteger) = New List(Of
StringWithInteger)
GenericComboBox1.BindingSourceDataSource = mItemList
GenericComboBox1.SortItNow("Strng ASC")
Public Class GenericComboBox
Inherits System.Windows.Forms.ComboBox
Public BindingSource1 As BindingSource = New BindingSource()
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'.
 
R

RobinS

Branco Medeiros said:
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.
 
A

active

Thanks for such a complete explanation


Branco Medeiros said:
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'.
 
A

active

Unless you tell me it's not a good idea I'll continue to use SortedList
which appears to work
Thanks
 
R

Richard T. Edwards

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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top