Reputation: 17799
How to set selected element of DevExpress LookupEdit by item? I.e., given one object of the LookupEdit's datasource, set the selection of the LookupEdit based on it.
Upvotes: 1
Views: 23289
Reputation: 15
Here is a simple solution for select first value index: LookUpEdit1.EditValue=TryCast(LookUpEdit1.Properties.DataSource,DataTable).Row(0).Item(0).ToString
Upvotes: 1
Reputation: 17799
After some time, I found the answer in the DevExpress forums. Just let the ValueMember
property unassigned, and set the EditValue
property of the control to the item to be selected. Taking @DmitryG example:
var dataSource = new List<Person>();
p1 = new Person(){ ID=0, Name="John", Age=27 };
dataSource.Add(p1);
//...
dataSource.Add(new Person(){ ID=101, Name="Mary", Age=23 });
lookupEdit1.Properties.DataSource = dataSource;
lookupEdit1.Properties.DisplayMember = "Name";
//...
lookupEdit1.EditValue = p1;
In this situation, it will be bound to the reference to an item.
Upvotes: 1
Reputation: 17850
You should set the LookupEdit.EditValue property with value from your data source according to LookupEdit.ValueMember property.
Here is the sample that demonstrated this approach:
var dataSource = new List<Person> {
new Person(){ ID=0, Name="John", Age=27 },
//...
new Person(){ ID=101, Name="Mary", Age=23 },
};
lookupEdit1.Properties.DataSource = dataSource;
lookupEdit1.Properties.DisplayMember = "Name";
lookupEdit1.Properties.ValueMember = "ID";
lookupEdit1.EditValue = 101; // Select Person with ID==101
//...
lookupEdit1.EditValue = lookupEdit1.Properties.GetDataSourceValue("ID", 1); // Select Person from second row by its ID
Upvotes: 4