简体   繁体   English

在ComboBox中显示对象属性

[英]Display object propery in ComboBox

I have a custom object which holds details about a project resource. 我有一个自定义对象,其中包含有关项目资源的详细信息。 Properties are PersonName, Position and Id If the resource isn't filled, PersonName is set to 'Unassgined'. 属性是PersonName,Position和Id。如果没有填写资源,PersonName将设置为“未确定”。

To add an object to a Combobox, I do: 要将对象添加到组合框,请执行以下操作:

    var avail = s.GetUnassignedPrintRoles(SprintId);
    foreach (var o in avail)
    {
        cmbRoles.Items.Add(o);
    }

This is fine when displaying a list of resources. 在显示资源列表时很好。 My object has an overridden ToString() method: 我的对象有一个重写的ToString()方法:

public override string ToString()
        {
            if(AssignedPerson != null)
                return ResourceType + " - " + AssignedPerson.Firstname + " " + AssignedPerson.Surname;
            return "Unassigned";
        }

But, I have a screen that shows a list of roles that are not assigned. 但是,我有一个屏幕,显示未分配角色的列表。 So, I get a list, where the Person is NULL. 因此,我得到一个列表,其中Person为NULL。

But, I want to display the 'Role' in the ComboxBox. 但是,我想在ComboxBox中显示“角色”。

But, my object's ToString shows 'Unassigned'. 但是,我对象的ToString显示为“未指定”。 How can I make it display the Role property? 如何使其显示Role属性? Is there a way to save the object in the Comboxbox item, but display a different propery in the display, other than what I have set in the ToString override? 有没有一种方法可以将对象保存在Comboxbox项中,但在显示中显示其他属性,而不是在ToString覆盖中设置的属性?

With regards to my comment, it might be needed to set the DisplayMember and ValueMember properties of the ComboBox, like so; 关于我的评论,可能需要像这样设置ComboBox的DisplayMemberValueMember属性。

cmbRoles.DisplayMember = "Role";
cmbRoles.ValueMember = "Id";
cmbRoles.DataSource = avail;

This way your ComboBox will Display the role, but the underlying data will be the ID, So when you select via the SelectedValue property, you'll get the ID. 这样,您的ComboBox将显示角色,但基础数据将是ID,因此,当您通过SelectedValue属性进行SelectedValue ,您将获得ID。

Have you tried using the DisplayMember property to distinguish the displayed value and the actual value? 您是否尝试过使用DisplayMember属性来区分显示值和实际值? If you do that, you should be able to set the Role as the displayed entry on the combobox. 如果这样做,则应该可以将“角色”设置为组合框上显示的条目。

cmbRoles.DisplayMember = "" + Role;
cmbRoles.ValueMember = "Id";
cmbRoles.DataSource = avail;

I'm not sure what Role is in your code but you should be able to get the gist from that. 我不确定代码中的角色是什么,但是您应该能够从中获得要点。

You can remove ToString() altogether by using read-only property: 您可以使用只读属性完全删除ToString():

public string FullInfo
{
    get
    {
       return ResourceType + " - " + AssignedPerson.Firstname + " " + AssignedPerson.Surname;
    }
}

then 然后

 comboBox.DisplayMember = "FullInfo";
 comboBox.ValueMember = "Id";
 comboBox.DataSource = avail;

and you can do any kind of properties like this. 您可以执行任何此类的属性。

Add this , 加上这个

private void InitializeComponent()
    {
      cmbRoles.ValueMember = "id"; 
      cmbRoles.DisplayMember = "discription";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM