Listbox, DataBinding & ValueMember Error

G

Guest

Working on a windows form, I am trying to bind a listbox to a dataset and
want to set the displaymember & Valuemember properties of the listbox so that
I can retrieve the selectedvalue from a selecteditem in the listbox.

My code is very simple:

lstReports.DataBindings.Clear()
lstReports.DataSource = dsDataSet.Tables("Reports")
lstReports.DisplayMember = "ReportDescription"
lstReports.ValueMember = "ReportsKey"


However, I keep getting the following error:
"Cannot modify the Items collection when the DataSource property is set."

and my application crashes.

I have used the displaymember & valuemember successfully with comoboboxes
and I have read the MSDN Help which suggests I should be able to do the same
with a listbox, but unfortunately I get this error.

Has any else come across this?
Any idea's as to how I resolve this?

Any help would really appreciated.
 
C

Cor Ligthert

Supa,

The listbox has a itemscollection. Probably are you setting/changing that
one somewhere.

(In the designer is the name collection).

I hope this helps

Cor
 
G

Guest

Thanks for the suggestion, but it wasn't that.

I had to re-organize the order in which I declared the value member, the
display member & the datasource. Once I put them in the following order, the
data binding worked ok.

lstReports.DataBindings.Clear()
lstReports.ValueMember = "ReportsKey"
lstReports.DataSource = dsDataSet.Tables("Reports")
lstReports.DisplayMember = "ReportDescription"

Strange, but true !!
 

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