C# DevExpress LookupEdit设置EditValue不起作用

C# DevExpress LookupEdit设置EditValue不起作用,c#,devexpress,C#,Devexpress,我试图让LookUpEdit在显示表单时显示初始值。我将绑定一个国家列表作为数据源,然后在表单加载时设置EditValue,该值应在LookUpEdit中将国家显示为所选项目。不幸的是,它只显示一个空值。LookUpEdit似乎可以正常工作,允许我在国家列表中滚动并选择一个项目,当我提交表单时,该值会传回 国家级: public class Country { public Country(); public int CountryId {get; set;} publi

我试图让LookUpEdit在显示表单时显示初始值。我将绑定一个国家列表作为数据源,然后在表单加载时设置EditValue,该值应在LookUpEdit中将国家显示为所选项目。不幸的是,它只显示一个空值。LookUpEdit似乎可以正常工作,允许我在国家列表中滚动并选择一个项目,当我提交表单时,该值会传回

国家级:

public class Country
{
    public Country();
    public int CountryId {get; set;}
    public string CountryName {get; set;}
    public string IsoCode {get; set; }
}
包含LookUpEdit的表单后面的代码:

this.country.Properties.DataSource = this.Countries;
this.country.Properties.DisplayMember = "CountryName";
this.country.Properties.ValueMember = "CountryId";

this.country.EditValue = initialCountry;
this.country.DataBindings.Add("EditValue", viewModel.Address, "AddressCountry", false, DataSourceUpdateMode.OnPropertyChanged);
在本例中,
this.Countries
是一个填充的
列表,
initialCountry
被设置为
Country
viewModel的一个实例。Address
包含一个属性
Country AddressCountry


我已经尝试过只直接设置
EditValue
,也尝试过自己将数据绑定设置为EditValue。无论我尝试什么,当表单加载时,LookUpEdit始终为空,我需要将其设置为
initialCountry
。我确信它非常简单,但我没有看到它,因此非常感谢您的帮助。

您不应该将
this.country.EditValue
设置为
国家的实例,而是设置为
国家ID
,因为这是您的
ValueMember

this.country.EditValue = initialCountry.CountryId;
编辑:如果要获取所选对象,应使用
GetDataSourceRowByKeyValue

var selectedCountry = this.country.GetDataSourceRowByKeyValue(this.country.EditValue) as Country;

除了Marko的回答之外:

有一种特殊的查找模式:

this.country.Properties.DataSource = this.Countries;
this.country.Properties.DisplayMember = "CountryName";
this.country.Properties.KeyMember = "CountryId";
this.country.EditValue = initialCountry;
此模式允许查找机制通过指定给属性的键字段(“CountryId”),查找编辑器值(一个
国家
业务对象)与查找数据源中另一个
国家
业务对象之间的匹配

以下是此模式的一些附加好处:

  • 您可以使用多个关键字段(“复合/复合关键点”功能)

    //用“;”分隔的字段名字符

    this.city.Properties.keymber=“CountryId;RegionId;CityName”

  • 您可以匹配从单独的数据上下文加载的业务对象,并使用延迟加载方法的所有优点:

    //CountryId值足够匹配。

    //加载时可以跳过所有其他字段(例如CountryName)

    this.country.EditValue=new country(){CountryId=5}


编辑值需要与数据库中的数据类型完全相同的数据,
因此,使用适当数据类型的转换功能分配数据

谢谢您的回答。有趣的是,这使得初始值起作用,但现在当表单提交时,只有CountryId在Country类上更新为正确的值。我希望整个对象将更新为LookUpEdit中选定的Country对象。有什么想法吗?谢谢你的帮助,已经解决了!谢谢你,我不知道这个功能!是的,好了,DX的家伙们正在不断改进WinForms的所有产品线,我很高兴LookUpEdit是一个有一些新技巧的老家伙。)谢谢,这看起来很有用!设置
keymber
似乎是一个技巧,糟糕的是,这不在文档中,我已经找了几个小时了