Listbox.DisplayMember won't display value from calculated property
I'm using SubSonic 2.2 for my DAL and extended one of my classes with a calculated property that returns a string containing another property with indenting based on the level of the outline at which the item occurs. The code for the property is below. The problem is that when I try to use this property as the DisplayMember for a ListBox control on a form (the reason I wrote it in the first place) it won't work. The ListBox reverts to displaying the ID property which is set as the ValueMember. To test that the property was working I looped through the collection of objects that I was populating the ListBox with and, using MessageBox.Show(obj.property), confirmed that it was indeed returning the value I'm looking for. Am I missing something or should this work? btw - There may be a better way to do the indenting but that's not what I'm after at the moment, thanks!
Code follows:
public partial class InteriorsCategory : ActiveRecord, IActiveRecord { public string ListDisplay { get { string returnValue = "";
for (int i = 1; i < this.SpecLevel; i++) { returnValue += " "; } returnValue += this.CategoryName; return returnValue; } } }
<>
I definitely get data in my collection and the binding I'm doing is exactly the same as yours (binding code posted below). The return value of the ListDisplay property that I'm using is a string concatenation of two values in the object. Think of it as a "full name" property that concatenates the FirstName a space and the LastName properties into a single string which it returns. I am trying to bind the ListDisplay property to the DisplayMember property of the listbox, but all that shows in the listbox is the Id field which I am binding to the ValueMember.
private void FillCategories() { lstPackageCategories.DataSource = new InteriorsCategoryCollection().Load(); lstPackageCategories.DisplayMember = "CategoryName"; 开发者_JAVA技巧 lstPackageCategories.ValueMember = "Id";
((InteriorsCategoryCollection)(lstPackageCategories.DataSource)).Sort("SpecSection", true); lstPackageCategories.SelectedItem = lstPackageCategories.Items[0];currentCategory = (InteriorsCategory)lstPackageCategories.SelectedItem; RefreshAvailableItems(); }
If your able to see your data in the collection, then it sounds like there is a problem on the binding of your ListBox. Here is an example of how I bind a ListBox using a SubSonic collection of values.
ISOCountryCodeCollection countrys =
new ISOCountryCodeCollection().OrderByAsc(ISOCountryCode.Columns.Country).Load();
Country.DataSource = countrys;
Country.DataValueField = "ThreeChar";
Country.DataTextField = "Country";
Country.DataBind();
In the example above, I'm binding the 3 character country code to the "DataValueField" and the full country name to the "DataTextField".
精彩评论