Composite DisplayMember for ComboBox
I need to display multiple pieces of data in a combobox, but I can't figure out how to do it.
Here's the code I'm trying to make work:
innerBox.DisplayMember = @"t => t.TenantName + ""\t"" + t.Property.PropertyName + ""\t"" + t.RentalUnit.UnitNumber ";
But it doesn't work, this does though:
innerBox.DisplayMem开发者_Python百科ber = @"t => t.TenantName";
How can I get the composite one to work?
This is not possible.
Instead, you should add a property to your underlying objects.
DisplayMember can contain only single property name! If you want composite output, you should subscribe to Format event and compose output string in code.
I wanted to have "[code] [text]' in DisplayMember and solved it by adding a property using linq:
var actionCodes = pps.GetAllActionCodes();
if (actionCodes != null)
{
var actionCodesNew = (from c in actionCodes
select new
{
c.Code,
c.Text,
CodeAndDesc = string.Format("{0} {1}", c.Code, c.Text).Trim()
}).ToArray();
comboBox.Items.AddRange(actionCodesNew);
comboBox.DisplayMember = "CodeAndDesc";
}
}
Works ok when performance is not an issue. :)
精彩评论