471,349 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Listbox, DataBinding & ValueMember Error

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.
Jul 21 '05 #1
2 1864
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
Jul 21 '05 #2
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 !!

"Cor Ligthert" wrote:
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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dave | last post: by
2 posts views Thread by Supa Hoopsa | last post: by
3 posts views Thread by Dan Slaby | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.