Combo short list not displayed

  • Thread starter Thread starter mklapp
  • Start date Start date
M

mklapp

Hello,

I have a combobox and am binding a dataset thusly:

combobox1.datasource = dataset1
combobox1.displaySource = "STUDENTS.StudentId"

There are only two records in the dataset
table "STUDENTS". These two rows do not appear (except
as noted below). If I supply a much larger dataset
(hundreds or thousands) the list appears as it should.

Note:
If I exit this form (a converted VB6 App with several
forms), and reenter it from a 'menu' form that has never
been closed (part of the same app), the combo box is
displaying the two rows.

mklapp
 
Have you tried binding using code similar to the code below?
ComboBox1.DataSource = DataSet1.Tables("STUDENTS")
ComboBox1.DisplayMember = "StudentId"
 
Hello,

Yes, I Have...

In Debug mode, when I do as you suggest, the Immediate
window reports, in response to :

?combobox1.datasource
The value :
Nothing

And, as I said, if there are 'many' items (some
number more than 2), the list appears quite nicely.

Mklapp
 
Then it sounds like you have an issue somewhere else in your code that is
causing the DataSource to be reset to Nothing. Try single stepping through
the debugger (if you haven't already) and try commenting out some source
code in the application that might be causing this. If you still can't find
the problem then create a smaller application that just reads from your
database and assigns to a DataGrid. At least doing something like this will
let you know if your data retrieval and binding logic are correct, and then
you can continue to determine what in your application is causing this
behavior. This has to be something in your code.
 
Nothing else is setting the property to nothing. In the
debugger the line:

cboStudentVisitsId.DataSource = tiDS.tables("IDS")

is executed. After it is executed (just b4 the next
line is executed), the DataSource is 'Nothing'.

I have single stepped through the code and unless the
code is :
cboStudentVisitsId.DataSource = tiDS
cboStudentVisitsId.DisplayMember = "IDS.StudentId"

The datasource property does not retain the set value
(In this case, the datatable "IDS". The above settings
result in a good combo box, but it appears as though
there must be some number of items larger than two.

I have created a smaller test application that uses
the lines above and it works fine, but there are many
more than two items in that combo box.
 
Can you post some more code? This has to be something to you're doing (or
not doing) somewhere else in your code. This should be a relatively simple
task:
(1) Populate a DataTable with data from some sort of data source (ie.
database, xml file, etc.)
(2) Bind the DataTable to the DataSource property of a control (in this case
ComboBox)
(3) Tell the control what column to display from the DataTable (in this case
using DisplayMember)

Does the DataTable contain rows? Can you loop through and display the
"StudentId" for each record in the DataTable?
 
Hello,

The problem is the Sorted Property of the ComboBox.
If it is true, it seems to work as I have described. If
it is False, All works well.

Thanks for your input. It was helpful.

mklapp
 
I have had this problem before, binding to a ComboBox that is Sorted = True,
but you should get an exception for this. From the MSDN docs:
"ArgumentException: An attempt was made to sort a ComboBox that is attached
to a data source."

Didn't you get an exception?
 
No. The sequence was :

1. fill the dataset. The underlying data was already
sorted.

2. Assign it to the combo box.

The Sorted Property appears to have been set by the VB6
upgrade wizard. If I draq a combo box to a form, the
default Sorted Property is false.
I made no explicit sort call nor do I set the Sorted
Property (The Generated windows code (designer or
upgrade) does this.

I have located the MSDN article you refer to (It's easier
when you know what the problem is), it speaks only to
setting it("Sorted") after the datasource property is
set. Apparently the ComboBox has other issues (at the
very least). Maybe we'll see another article.

mklapp
 
But the problem was the Sorted property and that has been solved, correct?
So everything is ok now. It's just that whenever I have sorted a ComboBox
and then later changed from manually filling it to data binding to the
DataSource property (and I have forgot to change the Sorted back to False)
it has always thrown an exception. So it seems to be strange behavior that
it didn't throw an exception for you. But anyways problem solved, right?
 
Oh yes. That is fine.

Thanks,

mklapp
-----Original Message-----
But the problem was the Sorted property and that has been solved, correct?
So everything is ok now. It's just that whenever I have sorted a ComboBox
and then later changed from manually filling it to data binding to the
DataSource property (and I have forgot to change the Sorted back to False)
it has always thrown an exception. So it seems to be strange behavior that
it didn't throw an exception for you. But anyways problem solved, right?

--
Tim Wilson
..Net Compact Framework MVP




.
 
Back
Top