peacemaker
peacemaker

Reputation: 2591

DevExpress LookupEdit setting EditValue does not work

I'm trying to get the LookUpEdit to show an initial value when the form is displayed. I'm binding a list of countries as the datasource and then setting the EditValue when the form loads which should show the country as the selected item in the LookUpEdit. Unfortunately, it just shows an empty value. The LookUpEdit appears to otherwise work and allows me to scroll through the list of countries and select an item and the value is passed back when I submit the form.

The Country class:

public class Country
{
    public Country();
    public int CountryId {get; set;}
    public string CountryName {get; set;}
    public string IsoCode {get; set; }
}

The code behind the form containing the 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);

In this example this.Countries is a populated List<Country> and initialCountry is set to an instance of Country and viewModel.Address contains a property Country AddressCountry.

I've tried both setting the EditValue directly only and setting the data binding to EditValue on it's own as well. Whatever I try, the LookUpEdit is always blank when the form loads and I need it to be set to the initialCountry. I'm sure it's something really simple but I'm not seeing it so any help is much appreciated.

Upvotes: 3

Views: 9039

Answers (3)

sandeep b j
sandeep b j

Reputation: 1

Edit Value requires Data with Exact Data Type as it in Database, so assign data with convert function of appropriate data type

Upvotes: 0

DmitryG
DmitryG

Reputation: 17850

In addition to the Marko's answer:

There is a special mode of data binding to the entire business objects in lookup:

this.country.Properties.DataSource = this.Countries;
this.country.Properties.DisplayMember = "CountryName";
this.country.Properties.KeyMember = "CountryId";
this.country.EditValue = initialCountry;

This mode allows the lookup mechanism to find a match between the editor's value (a Country business object) and another Country business objects in the lookup data source, via the key field ("CountryId") is assigned to the RepositoryItemLookUpEditBase.KeyMember property.

Here are some additional benefits of this mode:

  • you can use multiple key fields ("composite/compound key" feature);

    // field names delimited with the ';' character
    this.city.Properties.KeyMember = "CountryId;RegionId;CityName";

  • you can match the business objects, loaded from the separate data-contexts, and use all the advantages of lazy-loading approach:

    // The CountryId value is enough for match.
    // All the another fields(e.g. CountryName) can be skipped while loading
    this.country.EditValue = new Country() { CountryId = 5 }

Upvotes: 6

Marko Juvančič
Marko Juvančič

Reputation: 5890

You should not set this.country.EditValue to an instance of Country, but to CountryId, since this is your ValueMember.

this.country.EditValue = initialCountry.CountryId;

EDIT: if you want to get the selected object you should use GetDataSourceRowByKeyValue

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

Upvotes: 3

Related Questions