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 / June 2008

Tip: Looking for answers? Try searching our database.

bind listbox to a generic arraylist of a class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Smith - 25 Jun 2008 11:17 GMT
Hi, I have a generic arraylist of a class as follows:

List<MergeItem> lstSelectedItems;

public class MergeItem
   {
       public string Id;
       public string Name;

       public MergeItem(string id, string name)
       {
           Id = id;
           Name = name;

       }

   }

In form load we have

this.lstSelectedItems = new List<MergeItem>();

I add Items to the generic list as follows:

private void MoveSelectedRowToList(DataGridViewRow dr)
       {

           this.lstSelectedItems.Add(new
MergeItem(Convert.ToString(dr.Cells[0].Value),Convert.ToString(dr.Cells[1].Value)));
           lbSelected.DataSource = null;
           lbSelected.DataSource = this.lstSelectedItems;

         
       }

However this just shows MergeItems.MergeItems in the list box, How do I get
the
Displayed value to be the Name value from the MergeItem class and the
valuemember to be the Id field of the class.

lbSelected.ValueMember = "Id";  gave me an error.

Thanx in advance

Robert
Jack Jackson - 25 Jun 2008 17:14 GMT
>Hi, I have a generic arraylist of a class as follows:
>
[quoted text clipped - 41 lines]
>
>Robert

You can only bind to properties.  Change MergeItem so that Id and Name
are properties instead of fields, then set the listbox's DisplayMember
and ValueMember to "Id" and "Name".

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.