Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / February 2005

Tip: Looking for answers? Try searching our database.

ListControl DisplayMember being reset after DataSource change

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nat Wallbank - 14 Feb 2005 10:48 GMT
Has anyone else noticed the following problem?

I like to set my DisplayMember and ValueMember properties of my
ListControls before setting the DataSource (in fact I only set them
once) so that my SelectedIndexChanged event only fires once.  When I
set the DataSource to a custom collection of mine, everything works
fine as long as the collection actually contains at least one item.

If I set the DataSource to an empty collection (or null),
DisplayMember is reset (== string.Empty), but ValueMember remains as
it was.

There are obviously a number of workarounds for this (e.g. re-setting
DisplayMember after setting DataSource to an empty collection or null,
or I could just always set DisplayMember after DataSource but then I
get an extra SelectedIndexChanged event).

I was just wondering whether this is a known bug, or whether there is
a good reason for it..
Tom Krueger [MSFT] - 22 Feb 2005 22:01 GMT
Hi Nat,

To start, you are correct in setting the DataSource after the DispalyMember
and ValueMember.  This is more efficient because the control only get bound
once instead of 3 times.  You are also correct in that if the datasource
becomes null the DisplayMember will be reset to string.Empty.

The two things I can think of is.  The first is redesign so that you the
datasource does not become null.  Use a datasource that can be empty without
being null like an ArrayList or DataTable.  The second is that, as you said,
set the DisplayMember each time you set the bound datasource object to
something.  In this case I would use a method that sets the bound datasource
object where you include setting the DisplayMember value so that it is in
one spot.

Note: I don't believe by setting the DisplayMember after the DataSource will
work for you. You would still run into the same problem of the DispalyMember
being reset.

Take care,

Tom Krueger

My Blog - http://weblogs.asp.net/tom_krueger
Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.

> Has anyone else noticed the following problem?
>
[quoted text clipped - 15 lines]
> I was just wondering whether this is a known bug, or whether there is
> a good reason for it..
Nat Wallbank - 28 Feb 2005 15:48 GMT
Hi Tom,

Thanks for your reply.  I was actually using a custom collection
(based on CollectionBase), so it was never actually null (I just
tested the DataSource with null to see if I got the same effect).

In the end, when setting the DataSource property I think I just
checked whether the current DataSource was empty, and if so set
DisplayMember again before assigning the new DataSource.

Thanks again,

Nat

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.