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".