Home DevExpress Tips How to easily set the selected index or value for DevExpress lookupedit
Author:

DevExpress lookupedit works similar to the .NET combobox but when you want to set the desire item within the lookupedit it is a bit different to the combox. In this example you learn how to set the selected index for the lookupedit. Pay attention to the highlighted line.

private void PopulatePlants()

{

    DataTable dtPlants = new DataTable();

 

    DbHelper db = new DbHelper();

    db.StoredProcedure = "dbo.prPlantsSelect";

    dtPlants = db.GetDataTable();

    db.Close();

    cbPlant.Properties.DisplayMember = "Description";

    cbPlant.Properties.ValueMember = "ID";

    cbPlant.Properties.DataSource = dtPlants;

 

    //Set the desire index after the lookup has been populated.

    cbPlant.EditValue = cbPlant.Properties.GetKeyValueByDisplayText("TheSelectedIndexYouWant");

}

 



Comments (6)
  • shahab  - help
    hi, it doesn't work!!!!!!
    personLookUpEdit.EditValue = personLookUpEdit.Properties.GetDataSourceRowByKeyValue(2);
    -------------
    private void PersonLookUpEditBoxBind()
    {
    try
    {
    BindingSource _BindingSource;
    DataSet _DataSet = new DataSet();
    DataTable _DataTable = new PersonBLL().SelectLookUpEdit();
    _DataTable.TableName = "PERSON";
    _DataSet.Tables.Add(_DataTable);
    _BindingSource = new BindingSource(_DataSet, "PERSON");
    personLookUpEdit.DataBindings.Add("EditValue", _BindingSource, "PERSONID");

    personLookUpEdit.Properties.DataSource = _BindingSource;
    personLookUpEdit.Properties.DisplayMember = "FULLNAME";
    personLookUpEdit.Properties.ValueMember = "PERSONID";
    LookUpColumnInfoCollection _LookUpColumnInfoCollection = personLookUpEdit.Pr...
  • admin
    Have you check to make sure that it returns the correct data? because it works for me.
  • jayaram  - set selected item lookupedit
    you should be passing in the text as it appears in the drop down instead of the index to GetKeyValueByDisplayText function
  • Ganesh
    hi

    thanks dude it works fine...

    am looking for this code for long time.

    nice work...
  • Nelson  - Another way
    If want to set according to da value member


    cbPlant.ItemIndex = cbPlant.Properties.GetDataSourceRowIndex("FIELD_ID", vFieldId);
  • Nahum Reyes
    This is the code that i was looking for!!!
    It´s Great!!

    You´re the Maximus brother !!! :0
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"