Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayMember property from base class in combobox

Tags:

c#

.net

combobox

I use Windows Forms. I want DisplayMember equal property from base class? I have class

public class MyViewModel
{
    public int Id { get; set; }
    public Type Type { get; set; }
}

I want, that my comboBox show Type.Name.

List<MyViewModel> list = new List<MyViewModel>();
list.Add(new MyViewModel(){ Id = 1, Type.GetType(int)});
list.Add(new MyViewModel(){ Id = 2, Type.GetType(string)});
//how i must to config displayMember???
myComboBox.DisplayMember = "Type.Name";
myComboBox.ValueMember = "Id";
myComboBox.DataSoutce = list;

But i can't get Type.Name for display in comboBox. Can you help me?

like image 210
isxaker Avatar asked Feb 02 '26 09:02

isxaker


1 Answers

A ComboBox has an event for this purpose called Format:

myComboBox.DisplayMember = "Type";//Notice this
myComboBox.Format += (s,e) => {
    e.Value = ((Type)e.Value).Name;
};
like image 117
King King Avatar answered Feb 04 '26 21:02

King King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!