Comboxboxes and the sorted property

G

Guest

Discovered a fun one that I thought I should share, and hopefully catch
Microsoft's attention to update some documentation.

I'm a bit new to VB.NET, and was struggling with getting a combo box to
work. I was attempting to take advantage of the cool way to load a combo box
via a datasource, rather than the old VB6 way of adding selections.

First I tried an array of objects. I had some issues with that that I
suspect had a lot to do with not overloading ToString on the class so the
combo box would return the right thing for the displaymember item. But I had
also read some bugs around arrays and combo boxes, so I switched to a
disconnected dataset.

I had a heck of a time getting the combo box to load properly. I kept
getting either no data in the combo box, or it kept showing the phrase
"System.Data.DataRowView". I found out that you have to set the datasource
property first, before you set the datamember and the valuemember properties.

But the real kicker was that you can't use the datasource and get it to work
if the sorted property on the combo box is set to true. It appears that if
you set that property to true, then the combo box assumes you're going to do
things the old VB6 way and use the Add items method to the combo box. My
tipoff something was wrong that I'd watch the debugger, and right after the
statement like this:

cbTest.DataSource = dsInfo.Tables(0)

the value of cbTest.Datasource was "nothing", like the assignment didnt take.

In order to get things to work the way I wanted them to, I simply built a
dataview that sorted the table for me, and fed that to the combobox with the
sorted value off, and it finally worked. Three weeks later, of course.

Would someone from Microsoft confirm that you shouldn't use datasource in
combo and list boxes if you set the Sorted property to true in the form
designer? If that is the case, note that somewhere in the MSDN information
about the ListBox and the ComboBox classes!! It's not mentioned in the
MSPress books either (I have Programming Microsoft Visual Basic.Net, the 2003
version, ISBN 0-7356-2059-8)
 
M

Mohamoss

Hi
Actually this behavior that you are experiencing is the expected one. When
you try to set a combobox as sorted while it is connected to a data source
(the sort criteria e configured) this actually sort an exception
(ArgumentException) which is thrown under the condition (An attempt was
made to sort a ComboBox that is attached to a data source.) this is
actually documented on the MSDN page the of the sorted property on combobox
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscomboboxclasssortedtopic.asp . so what happens here
is that the binding is never done as the exception is raised .

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
G

Guest

Three things:

1) The exception is not being raised to the application level, or if it is,
its being handled internal to the libraries, at least in VB.NET 2003. I never
saw it. What I saw was an empty combo box. If you'd like, I can send a very
short code snippet in VB.NET that demonstrates this, but you should be able
to recreate it very easily.

2) I would strongly reccommend that you add a comment in the Remarks section
of the iDataSource page of the ComboBox and ListBox classes that states "do
not use the sorted property when using a data source". When I was trying to
solve this problem, I looked in that topic for something I was doing wrong.
It was a coincidence that I stumbled on the sorted property being the cause.

3) I would suggest that you modify the section in the Sorted property of the
class description to clearly state that this property is to be used only when
using the add method.
 

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